Skip to content

Commit df1ec91

Browse files
chaseruskinkyoto7250
authored andcommitted
adds lint logic and test for bytes_count_to_len
formats code with fixes single match clippy error to replace with if let swaps ident.name.as_str to ident.name == sym for count fn
1 parent 3abd2c0 commit df1ec91

8 files changed

+94
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3299,6 +3299,7 @@ Released 2018-09-13
32993299
[`boxed_local`]: https://rust-lang.github.io/rust-clippy/master/index.html#boxed_local
33003300
[`branches_sharing_code`]: https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
33013301
[`builtin_type_shadow`]: https://rust-lang.github.io/rust-clippy/master/index.html#builtin_type_shadow
3302+
[`bytes_count_to_len`]: https://rust-lang.github.io/rust-clippy/master/index.html#bytes_count_to_len
33023303
[`bytes_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#bytes_nth
33033304
[`cargo_common_metadata`]: https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata
33043305
[`case_sensitive_file_extension_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#case_sensitive_file_extension_comparisons
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use clippy_utils::diagnostics::span_lint_and_note;
2+
use if_chain::if_chain;
3+
use rustc_hir as hir;
4+
use rustc_lint::{LateContext, LateLintPass};
5+
use rustc_session::{declare_lint_pass, declare_tool_lint};
6+
7+
declare_clippy_lint! {
8+
/// ### What it does
9+
/// It checks for `str::bytes().count()` and suggests replacing it with
10+
/// `str::len()`.
11+
///
12+
/// ### Why is this bad?
13+
/// `str::bytes().count()` is longer and may not be as performant as using
14+
/// `str::len()`.
15+
///
16+
/// ### Example
17+
/// ```rust
18+
/// "hello".bytes().count();
19+
/// ```
20+
/// Use instead:
21+
/// ```rust
22+
/// "hello".len();
23+
/// ```
24+
#[clippy::version = "1.60.0"]
25+
pub BYTES_COUNT_TO_LEN,
26+
complexity,
27+
"Using bytest().count() when len() performs the same functionality"
28+
}
29+
30+
declare_lint_pass!(BytesCountToLen => [BYTES_COUNT_TO_LEN]);
31+
32+
impl<'tcx> LateLintPass<'tcx> for BytesCountToLen {
33+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
34+
if_chain! {
35+
//check for method call called "count"
36+
if let hir::ExprKind::MethodCall(count_path, count_args, _) = &expr.kind;
37+
if count_path.ident.name == rustc_span::sym::count;
38+
if let [bytes_expr] = &**count_args;
39+
//check for method call called "bytes" that was linked to "count"
40+
if let hir::ExprKind::MethodCall(bytes_path, _, _) = &bytes_expr.kind;
41+
if bytes_path.ident.name.as_str() == "bytes";
42+
then {
43+
span_lint_and_note(
44+
cx,
45+
BYTES_COUNT_TO_LEN,
46+
expr.span,
47+
"using long and hard to read `.bytes().count()`",
48+
None,
49+
"`.len()` achieves same functionality"
50+
);
51+
}
52+
};
53+
}
54+
}

clippy_lints/src/lib.register_all.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
2323
LintId::of(bool_assert_comparison::BOOL_ASSERT_COMPARISON),
2424
LintId::of(booleans::LOGIC_BUG),
2525
LintId::of(booleans::NONMINIMAL_BOOL),
26+
LintId::of(bytes_count_to_len::BYTES_COUNT_TO_LEN),
2627
LintId::of(casts::CAST_ABS_TO_UNSIGNED),
2728
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
2829
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
@@ -64,6 +64,7 @@ store.register_lints(&[
6464
booleans::NONMINIMAL_BOOL,
6565
borrow_as_ptr::BORROW_AS_PTR,
6666
bytecount::NAIVE_BYTECOUNT,
67+
bytes_count_to_len::BYTES_COUNT_TO_LEN,
6768
cargo::CARGO_COMMON_METADATA,
6869
cargo::MULTIPLE_CRATE_VERSIONS,
6970
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;
@@ -875,6 +876,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
875876
store.register_late_pass(|| Box::new(unnecessary_owned_empty_strings::UnnecessaryOwnedEmptyStrings));
876877
store.register_early_pass(|| Box::new(pub_use::PubUse));
877878
store.register_late_pass(|| Box::new(format_push_string::FormatPushString));
879+
store.register_late_pass(|| Box::new(bytes_count_to_len::BytesCountToLen));
878880
// add lints here, do not remove this comment, it's used in `new_lint`
879881
}
880882

tests/ui/bytes_count_to_len.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![warn(clippy::bytes_count_to_len)]
2+
3+
fn main() {
4+
let s1 = String::from("world");
5+
6+
//test warning against a string literal
7+
"hello".bytes().count();
8+
9+
//test warning against a string variable
10+
s1.bytes().count();
11+
12+
//make sure using count() normally doesn't trigger warning
13+
let vector = [0, 1, 2];
14+
let size = vector.iter().count();
15+
}

tests/ui/bytes_count_to_len.stderr

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: using long and hard to read `.bytes().count()`
2+
--> $DIR/bytes_count_to_len.rs:7:5
3+
|
4+
LL | "hello".bytes().count();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::bytes-count-to-len` implied by `-D warnings`
8+
= note: `.len()` achieves same functionality
9+
10+
error: using long and hard to read `.bytes().count()`
11+
--> $DIR/bytes_count_to_len.rs:10:5
12+
|
13+
LL | s1.bytes().count();
14+
| ^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: `.len()` achieves same functionality
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)