-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint bytes_count_to_len
#8375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
chaseruskin
wants to merge
6
commits into
rust-lang:master
from
chaseruskin:new-lint-bytes-count-to-len
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
befd434
adds lint logic and test for bytes_count_to_len
chaseruskin 107a350
formats code with
chaseruskin 681187c
formats rust code with 'dev fmt'
chaseruskin 5996e63
Merge branch 'new-lint-bytes-count-to-len' of https://github.com/c-ru…
chaseruskin 8fd5fec
fixes single match clippy error to replace with if let
chaseruskin 20ca570
swaps ident.name.as_str to ident.name == sym for count fn
chaseruskin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use clippy_utils::diagnostics::span_lint_and_note; | ||
use if_chain::if_chain; | ||
use rustc_hir as hir; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// It checks for `str::bytes().count()` and suggests replacing it with | ||
/// `str::len()`. | ||
/// | ||
/// ### Why is this bad? | ||
/// `str::bytes().count()` is longer and may not be as performant as using | ||
/// `str::len()`. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// "hello".bytes().count(); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// "hello".len(); | ||
/// ``` | ||
#[clippy::version = "1.60.0"] | ||
pub BYTES_COUNT_TO_LEN, | ||
complexity, | ||
"Using bytest().count() when len() performs the same functionality" | ||
} | ||
|
||
declare_lint_pass!(BytesCountToLen => [BYTES_COUNT_TO_LEN]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for BytesCountToLen { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { | ||
if_chain! { | ||
//check for method call called "count" | ||
if let hir::ExprKind::MethodCall(count_path, count_args, _) = &expr.kind; | ||
if count_path.ident.name == rustc_span::sym::count; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use And we should ensure the type is |
||
if let [bytes_expr] = &**count_args; | ||
//check for method call called "bytes" that was linked to "count" | ||
if let hir::ExprKind::MethodCall(bytes_path, _, _) = &bytes_expr.kind; | ||
if bytes_path.ident.name.as_str() == "bytes"; | ||
then { | ||
span_lint_and_note( | ||
cx, | ||
BYTES_COUNT_TO_LEN, | ||
expr.span, | ||
"using long and hard to read `.bytes().count()`", | ||
None, | ||
"`.len()` achieves same functionality" | ||
); | ||
} | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#![warn(clippy::bytes_count_to_len)] | ||
|
||
fn main() { | ||
let s1 = String::from("world"); | ||
|
||
//test warning against a string literal | ||
"hello".bytes().count(); | ||
|
||
//test warning against a string variable | ||
s1.bytes().count(); | ||
|
||
//make sure using count() normally doesn't trigger warning | ||
let vector = [0, 1, 2]; | ||
let size = vector.iter().count(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error: using long and hard to read `.bytes().count()` | ||
--> $DIR/bytes_count_to_len.rs:7:5 | ||
| | ||
LL | "hello".bytes().count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::bytes-count-to-len` implied by `-D warnings` | ||
= note: `.len()` achieves same functionality | ||
|
||
error: using long and hard to read `.bytes().count()` | ||
--> $DIR/bytes_count_to_len.rs:10:5 | ||
| | ||
LL | s1.bytes().count(); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `.len()` achieves same functionality | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.