|
1 | 1 | mod bind_instead_of_map;
|
2 | 2 | mod bytecount;
|
| 3 | +mod bytes_count_to_len; |
3 | 4 | mod bytes_nth;
|
4 | 5 | mod chars_cmp;
|
5 | 6 | mod chars_cmp_with_unwrap;
|
@@ -2402,6 +2403,31 @@ declare_clippy_lint! {
|
2402 | 2403 | "use of naive `<slice>.filter(|&x| x == y).count()` to count byte values"
|
2403 | 2404 | }
|
2404 | 2405 |
|
| 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 | + |
2405 | 2431 | pub struct Methods {
|
2406 | 2432 | avoid_breaking_exported_api: bool,
|
2407 | 2433 | msrv: Option<RustcVersion>,
|
@@ -2507,6 +2533,7 @@ impl_lint_pass!(Methods => [
|
2507 | 2533 | ITER_ON_SINGLE_ITEMS,
|
2508 | 2534 | ITER_ON_EMPTY_COLLECTIONS,
|
2509 | 2535 | NAIVE_BYTECOUNT,
|
| 2536 | + BYTES_COUNT_TO_LEN, |
2510 | 2537 | ]);
|
2511 | 2538 |
|
2512 | 2539 | /// Extracts a method call name, args, and `Span` of the method name.
|
@@ -2768,6 +2795,7 @@ impl Methods {
|
2768 | 2795 | },
|
2769 | 2796 | Some(("map", [_, arg], _)) => suspicious_map::check(cx, expr, recv, arg),
|
2770 | 2797 | Some(("filter", [recv2, arg], _)) => bytecount::check(cx, expr, recv2, arg),
|
| 2798 | + Some(("bytes", [recv2], _)) => bytes_count_to_len::check(cx, expr, recv, recv2), |
2771 | 2799 | _ => {},
|
2772 | 2800 | },
|
2773 | 2801 | ("drain", [arg]) => {
|
|
0 commit comments