Skip to content

Commit ba6a459

Browse files
committed
Move BytesCountToLen into Methods lint pass
1 parent 2502898 commit ba6a459

File tree

7 files changed

+68
-75
lines changed

7 files changed

+68
-75
lines changed

clippy_lints/src/bytes_count_to_len.rs

-70
This file was deleted.

clippy_lints/src/lib.register_all.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
2020
LintId::of(booleans::NONMINIMAL_BOOL),
2121
LintId::of(booleans::OVERLY_COMPLEX_BOOL_EXPR),
2222
LintId::of(borrow_deref_ref::BORROW_DEREF_REF),
23-
LintId::of(bytes_count_to_len::BYTES_COUNT_TO_LEN),
2423
LintId::of(casts::CAST_ABS_TO_UNSIGNED),
2524
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
2625
LintId::of(casts::CAST_ENUM_TRUNCATION),
@@ -151,6 +150,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
151150
LintId::of(mem_replace::MEM_REPLACE_WITH_DEFAULT),
152151
LintId::of(mem_replace::MEM_REPLACE_WITH_UNINIT),
153152
LintId::of(methods::BIND_INSTEAD_OF_MAP),
153+
LintId::of(methods::BYTES_COUNT_TO_LEN),
154154
LintId::of(methods::BYTES_NTH),
155155
LintId::of(methods::CHARS_LAST_CMP),
156156
LintId::of(methods::CHARS_NEXT_CMP),

clippy_lints/src/lib.register_complexity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
66
LintId::of(attrs::DEPRECATED_CFG_ATTR),
77
LintId::of(booleans::NONMINIMAL_BOOL),
88
LintId::of(borrow_deref_ref::BORROW_DEREF_REF),
9-
LintId::of(bytes_count_to_len::BYTES_COUNT_TO_LEN),
109
LintId::of(casts::CHAR_LIT_AS_U8),
1110
LintId::of(casts::UNNECESSARY_CAST),
1211
LintId::of(dereference::EXPLICIT_AUTO_DEREF),
@@ -33,6 +32,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
3332
LintId::of(matches::NEEDLESS_MATCH),
3433
LintId::of(matches::WILDCARD_IN_OR_PATTERNS),
3534
LintId::of(methods::BIND_INSTEAD_OF_MAP),
35+
LintId::of(methods::BYTES_COUNT_TO_LEN),
3636
LintId::of(methods::CLONE_ON_COPY),
3737
LintId::of(methods::FILTER_MAP_IDENTITY),
3838
LintId::of(methods::FILTER_NEXT),

clippy_lints/src/lib.register_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ store.register_lints(&[
5959
booleans::NONMINIMAL_BOOL,
6060
booleans::OVERLY_COMPLEX_BOOL_EXPR,
6161
borrow_deref_ref::BORROW_DEREF_REF,
62-
bytes_count_to_len::BYTES_COUNT_TO_LEN,
6362
cargo::CARGO_COMMON_METADATA,
6463
cargo::MULTIPLE_CRATE_VERSIONS,
6564
cargo::NEGATIVE_FEATURE_NAMES,
@@ -284,6 +283,7 @@ store.register_lints(&[
284283
mem_replace::MEM_REPLACE_WITH_DEFAULT,
285284
mem_replace::MEM_REPLACE_WITH_UNINIT,
286285
methods::BIND_INSTEAD_OF_MAP,
286+
methods::BYTES_COUNT_TO_LEN,
287287
methods::BYTES_NTH,
288288
methods::CHARS_LAST_CMP,
289289
methods::CHARS_NEXT_CMP,

clippy_lints/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ mod blocks_in_if_conditions;
180180
mod bool_assert_comparison;
181181
mod booleans;
182182
mod borrow_deref_ref;
183-
mod bytes_count_to_len;
184183
mod cargo;
185184
mod case_sensitive_file_extension_comparisons;
186185
mod casts;
@@ -907,7 +906,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
907906
store.register_late_pass(|| Box::new(unnecessary_owned_empty_strings::UnnecessaryOwnedEmptyStrings));
908907
store.register_early_pass(|| Box::new(pub_use::PubUse));
909908
store.register_late_pass(|| Box::new(format_push_string::FormatPushString));
910-
store.register_late_pass(|| Box::new(bytes_count_to_len::BytesCountToLen));
911909
let max_include_file_size = conf.max_include_file_size;
912910
store.register_late_pass(move || Box::new(large_include_file::LargeIncludeFile::new(max_include_file_size)));
913911
store.register_late_pass(|| Box::new(strings::TrimSplitWhitespace));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 if_chain::if_chain;
5+
use rustc_errors::Applicability;
6+
use rustc_hir as hir;
7+
use rustc_lint::LateContext;
8+
use rustc_span::sym;
9+
10+
use super::BYTES_COUNT_TO_LEN;
11+
12+
pub(super) fn check<'tcx>(
13+
cx: &LateContext<'tcx>,
14+
expr: &'tcx hir::Expr<'_>,
15+
count_recv: &'tcx hir::Expr<'_>,
16+
bytes_recv: &'tcx hir::Expr<'_>,
17+
) {
18+
if_chain! {
19+
if let Some(bytes_id) = cx.typeck_results().type_dependent_def_id(count_recv.hir_id);
20+
if let Some(impl_id) = cx.tcx.impl_of_method(bytes_id);
21+
if cx.tcx.type_of(impl_id).is_str();
22+
let ty = cx.typeck_results().expr_ty(bytes_recv).peel_refs();
23+
if ty.is_str() || is_type_diagnostic_item(cx, ty, sym::String);
24+
then {
25+
let mut applicability = Applicability::MachineApplicable;
26+
span_lint_and_sugg(
27+
cx,
28+
BYTES_COUNT_TO_LEN,
29+
expr.span,
30+
"using long and hard to read `.bytes().count()`",
31+
"consider calling `.len()` instead",
32+
format!("{}.len()", snippet_with_applicability(cx, bytes_recv.span, "..", &mut applicability)),
33+
applicability
34+
);
35+
}
36+
};
37+
}

clippy_lints/src/methods/mod.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod bind_instead_of_map;
22
mod bytecount;
3+
mod bytes_count_to_len;
34
mod bytes_nth;
45
mod chars_cmp;
56
mod chars_cmp_with_unwrap;
@@ -2402,6 +2403,31 @@ declare_clippy_lint! {
24022403
"use of naive `<slice>.filter(|&x| x == y).count()` to count byte values"
24032404
}
24042405

2406+
declare_clippy_lint! {
2407+
/// ### What it does
2408+
/// It checks for `str::bytes().count()` and suggests replacing it with
2409+
/// `str::len()`.
2410+
///
2411+
/// ### Why is this bad?
2412+
/// `str::bytes().count()` is longer and may not be as performant as using
2413+
/// `str::len()`.
2414+
///
2415+
/// ### Example
2416+
/// ```rust
2417+
/// "hello".bytes().count();
2418+
/// String::from("hello").bytes().count();
2419+
/// ```
2420+
/// Use instead:
2421+
/// ```rust
2422+
/// "hello".len();
2423+
/// String::from("hello").len();
2424+
/// ```
2425+
#[clippy::version = "1.62.0"]
2426+
pub BYTES_COUNT_TO_LEN,
2427+
complexity,
2428+
"Using `bytes().count()` when `len()` performs the same functionality"
2429+
}
2430+
24052431
pub struct Methods {
24062432
avoid_breaking_exported_api: bool,
24072433
msrv: Option<RustcVersion>,
@@ -2507,6 +2533,7 @@ impl_lint_pass!(Methods => [
25072533
ITER_ON_SINGLE_ITEMS,
25082534
ITER_ON_EMPTY_COLLECTIONS,
25092535
NAIVE_BYTECOUNT,
2536+
BYTES_COUNT_TO_LEN,
25102537
]);
25112538

25122539
/// Extracts a method call name, args, and `Span` of the method name.
@@ -2768,6 +2795,7 @@ impl Methods {
27682795
},
27692796
Some(("map", [_, arg], _)) => suspicious_map::check(cx, expr, recv, arg),
27702797
Some(("filter", [recv2, arg], _)) => bytecount::check(cx, expr, recv2, arg),
2798+
Some(("bytes", [recv2], _)) => bytes_count_to_len::check(cx, expr, recv, recv2),
27712799
_ => {},
27722800
},
27732801
("drain", [arg]) => {

0 commit comments

Comments
 (0)