Skip to content

Commit e17b97c

Browse files
committed
Auto merge of rust-lang#8711 - kyoto7250:new-lint-bytes-count-to-len, r=giraffate
Take over: New lint bytes count to len take over rust-lang#8375 close rust-lang#8083 This PR adds new lint about considering replacing `.bytes().count()` with `.len()`. Thank you in advance. --- r! `@Manishearth` changelog: adds new lint [`bytes_count_to_len`] to consider replacing `.bytes().count()` with `.len()`
2 parents cbdf17c + f19387d commit e17b97c

10 files changed

+174
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,7 @@ Released 2018-09-13
33003300
[`boxed_local`]: https://rust-lang.github.io/rust-clippy/master/index.html#boxed_local
33013301
[`branches_sharing_code`]: https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
33023302
[`builtin_type_shadow`]: https://rust-lang.github.io/rust-clippy/master/index.html#builtin_type_shadow
3303+
[`bytes_count_to_len`]: https://rust-lang.github.io/rust-clippy/master/index.html#bytes_count_to_len
33033304
[`bytes_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#bytes_nth
33043305
[`cargo_common_metadata`]: https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata
33053306
[`case_sensitive_file_extension_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#case_sensitive_file_extension_comparisons
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet_with_applicability;
3+
use clippy_utils::ty::is_type_diagnostic_item;
4+
use clippy_utils::{match_def_path, paths};
5+
use if_chain::if_chain;
6+
use rustc_errors::Applicability;
7+
use rustc_hir as hir;
8+
use rustc_lint::{LateContext, LateLintPass};
9+
use rustc_middle::ty;
10+
use rustc_session::{declare_lint_pass, declare_tool_lint};
11+
use rustc_span::sym;
12+
13+
declare_clippy_lint! {
14+
/// ### What it does
15+
/// It checks for `str::bytes().count()` and suggests replacing it with
16+
/// `str::len()`.
17+
///
18+
/// ### Why is this bad?
19+
/// `str::bytes().count()` is longer and may not be as performant as using
20+
/// `str::len()`.
21+
///
22+
/// ### Example
23+
/// ```rust
24+
/// "hello".bytes().count();
25+
/// String::from("hello").bytes().count();
26+
/// ```
27+
/// Use instead:
28+
/// ```rust
29+
/// "hello".len();
30+
/// String::from("hello").len();
31+
/// ```
32+
#[clippy::version = "1.62.0"]
33+
pub BYTES_COUNT_TO_LEN,
34+
complexity,
35+
"Using `bytes().count()` when `len()` performs the same functionality"
36+
}
37+
38+
declare_lint_pass!(BytesCountToLen => [BYTES_COUNT_TO_LEN]);
39+
40+
impl<'tcx> LateLintPass<'tcx> for BytesCountToLen {
41+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
42+
if_chain! {
43+
if let hir::ExprKind::MethodCall(_, expr_args, _) = &expr.kind;
44+
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
45+
if match_def_path(cx, expr_def_id, &paths::ITER_COUNT);
46+
47+
if let [bytes_expr] = &**expr_args;
48+
if let hir::ExprKind::MethodCall(_, bytes_args, _) = &bytes_expr.kind;
49+
if let Some(bytes_def_id) = cx.typeck_results().type_dependent_def_id(bytes_expr.hir_id);
50+
if match_def_path(cx, bytes_def_id, &paths::STR_BYTES);
51+
52+
if let [str_expr] = &**bytes_args;
53+
let ty = cx.typeck_results().expr_ty(str_expr).peel_refs();
54+
55+
if is_type_diagnostic_item(cx, ty, sym::String) || ty.kind() == &ty::Str;
56+
then {
57+
let mut applicability = Applicability::MachineApplicable;
58+
span_lint_and_sugg(
59+
cx,
60+
BYTES_COUNT_TO_LEN,
61+
expr.span,
62+
"using long and hard to read `.bytes().count()`",
63+
"consider calling `.len()` instead",
64+
format!("{}.len()", snippet_with_applicability(cx, str_expr.span, "..", &mut applicability)),
65+
applicability
66+
);
67+
}
68+
};
69+
}
70+
}

clippy_lints/src/lib.register_all.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
2424
LintId::of(bool_assert_comparison::BOOL_ASSERT_COMPARISON),
2525
LintId::of(booleans::LOGIC_BUG),
2626
LintId::of(booleans::NONMINIMAL_BOOL),
27+
LintId::of(bytes_count_to_len::BYTES_COUNT_TO_LEN),
2728
LintId::of(casts::CAST_ABS_TO_UNSIGNED),
2829
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
2930
LintId::of(casts::CAST_ENUM_TRUNCATION),

clippy_lints/src/lib.register_complexity.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec![
66
LintId::of(attrs::DEPRECATED_CFG_ATTR),
77
LintId::of(booleans::NONMINIMAL_BOOL),
8+
LintId::of(bytes_count_to_len::BYTES_COUNT_TO_LEN),
89
LintId::of(casts::CHAR_LIT_AS_U8),
910
LintId::of(casts::UNNECESSARY_CAST),
1011
LintId::of(derivable_impls::DERIVABLE_IMPLS),

clippy_lints/src/lib.register_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ store.register_lints(&[
6565
booleans::NONMINIMAL_BOOL,
6666
borrow_as_ptr::BORROW_AS_PTR,
6767
bytecount::NAIVE_BYTECOUNT,
68+
bytes_count_to_len::BYTES_COUNT_TO_LEN,
6869
cargo::CARGO_COMMON_METADATA,
6970
cargo::MULTIPLE_CRATE_VERSIONS,
7071
cargo::NEGATIVE_FEATURE_NAMES,

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ mod bool_assert_comparison;
181181
mod booleans;
182182
mod borrow_as_ptr;
183183
mod bytecount;
184+
mod bytes_count_to_len;
184185
mod cargo;
185186
mod case_sensitive_file_extension_comparisons;
186187
mod casts;
@@ -880,6 +881,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
880881
store.register_late_pass(|| Box::new(unnecessary_owned_empty_strings::UnnecessaryOwnedEmptyStrings));
881882
store.register_early_pass(|| Box::new(pub_use::PubUse));
882883
store.register_late_pass(|| Box::new(format_push_string::FormatPushString));
884+
store.register_late_pass(|| Box::new(bytes_count_to_len::BytesCountToLen));
883885
// add lints here, do not remove this comment, it's used in `new_lint`
884886
}
885887

clippy_utils/src/paths.rs

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub const IO_READ: [&str; 3] = ["std", "io", "Read"];
6161
pub const IO_WRITE: [&str; 3] = ["std", "io", "Write"];
6262
pub const IPADDR_V4: [&str; 5] = ["std", "net", "ip", "IpAddr", "V4"];
6363
pub const IPADDR_V6: [&str; 5] = ["std", "net", "ip", "IpAddr", "V6"];
64+
pub const ITER_COUNT: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "count"];
6465
pub const ITER_REPEAT: [&str; 5] = ["core", "iter", "sources", "repeat", "repeat"];
6566
#[allow(clippy::invalid_paths)] // internal lints do not know about all external crates
6667
pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"];
@@ -149,6 +150,7 @@ pub const STD_FS_CREATE_DIR: [&str; 3] = ["std", "fs", "create_dir"];
149150
pub const STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_str"];
150151
pub const STRING_AS_STR: [&str; 4] = ["alloc", "string", "String", "as_str"];
151152
pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"];
153+
pub const STR_BYTES: [&str; 4] = ["core", "str", "<impl str>", "bytes"];
152154
pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "<impl str>", "ends_with"];
153155
pub const STR_FROM_UTF8: [&str; 4] = ["core", "str", "converts", "from_utf8"];
154156
pub const STR_LEN: [&str; 4] = ["core", "str", "<impl str>", "len"];

tests/ui/bytes_count_to_len.fixed

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// run-rustfix
2+
#![warn(clippy::bytes_count_to_len)]
3+
use std::fs::File;
4+
use std::io::Read;
5+
6+
fn main() {
7+
// should fix, because type is String
8+
let _ = String::from("foo").len();
9+
10+
let s1 = String::from("foo");
11+
let _ = s1.len();
12+
13+
// should fix, because type is &str
14+
let _ = "foo".len();
15+
16+
let s2 = "foo";
17+
let _ = s2.len();
18+
19+
// make sure using count() normally doesn't trigger warning
20+
let vector = [0, 1, 2];
21+
let _ = vector.iter().count();
22+
23+
// The type is slice, so should not fix
24+
let _ = &[1, 2, 3].bytes().count();
25+
26+
let bytes: &[u8] = &[1, 2, 3];
27+
bytes.bytes().count();
28+
29+
// The type is File, so should not fix
30+
let _ = File::open("foobar").unwrap().bytes().count();
31+
32+
let f = File::open("foobar").unwrap();
33+
let _ = f.bytes().count();
34+
}

tests/ui/bytes_count_to_len.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// run-rustfix
2+
#![warn(clippy::bytes_count_to_len)]
3+
use std::fs::File;
4+
use std::io::Read;
5+
6+
fn main() {
7+
// should fix, because type is String
8+
let _ = String::from("foo").bytes().count();
9+
10+
let s1 = String::from("foo");
11+
let _ = s1.bytes().count();
12+
13+
// should fix, because type is &str
14+
let _ = "foo".bytes().count();
15+
16+
let s2 = "foo";
17+
let _ = s2.bytes().count();
18+
19+
// make sure using count() normally doesn't trigger warning
20+
let vector = [0, 1, 2];
21+
let _ = vector.iter().count();
22+
23+
// The type is slice, so should not fix
24+
let _ = &[1, 2, 3].bytes().count();
25+
26+
let bytes: &[u8] = &[1, 2, 3];
27+
bytes.bytes().count();
28+
29+
// The type is File, so should not fix
30+
let _ = File::open("foobar").unwrap().bytes().count();
31+
32+
let f = File::open("foobar").unwrap();
33+
let _ = f.bytes().count();
34+
}

tests/ui/bytes_count_to_len.stderr

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: using long and hard to read `.bytes().count()`
2+
--> $DIR/bytes_count_to_len.rs:8:13
3+
|
4+
LL | let _ = String::from("foo").bytes().count();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.len()` instead: `String::from("foo").len()`
6+
|
7+
= note: `-D clippy::bytes-count-to-len` implied by `-D warnings`
8+
9+
error: using long and hard to read `.bytes().count()`
10+
--> $DIR/bytes_count_to_len.rs:11:13
11+
|
12+
LL | let _ = s1.bytes().count();
13+
| ^^^^^^^^^^^^^^^^^^ help: consider calling `.len()` instead: `s1.len()`
14+
15+
error: using long and hard to read `.bytes().count()`
16+
--> $DIR/bytes_count_to_len.rs:14:13
17+
|
18+
LL | let _ = "foo".bytes().count();
19+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.len()` instead: `"foo".len()`
20+
21+
error: using long and hard to read `.bytes().count()`
22+
--> $DIR/bytes_count_to_len.rs:17:13
23+
|
24+
LL | let _ = s2.bytes().count();
25+
| ^^^^^^^^^^^^^^^^^^ help: consider calling `.len()` instead: `s2.len()`
26+
27+
error: aborting due to 4 previous errors
28+

0 commit comments

Comments
 (0)