-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
new lint to detect inefficient iter().any()
#13817
Open
lapla-cogito
wants to merge
5
commits into
rust-lang:master
Choose a base branch
from
lapla-cogito:contains_for_u8i8
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c52ebf2
new lint to use contains() instead of iter().any() for u8 and i8 slices
lapla-cogito c1d5108
rename contains_for_slice to slice_iter_any
lapla-cogito b3a1693
correct description for slice_iter_any
lapla-cogito 19357ca
add lint for checking unnecessary iter().any()
lapla-cogito e964cbc
add slice_iter_any lint
lapla-cogito 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 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 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 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 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,66 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use clippy_utils::msrvs::Msrv; | ||
use rustc_hir::{BinOpKind, Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{self}; | ||
use rustc_session::RustcVersion; | ||
use rustc_span::source_map::Spanned; | ||
|
||
use super::{SLICE_ITER_ANY, method_call}; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Msrv) { | ||
if !expr.span.from_expansion() | ||
// any() | ||
&& let Some((name, recv, args, _, _)) = method_call(expr) | ||
&& name == "any" | ||
// check if `iter().any()` can be replaced with `contains()` | ||
&& args.len() == 1 | ||
&& let ExprKind::Closure(closure) = args[0].kind | ||
&& let body = cx.tcx.hir().body(closure.body) | ||
&& let ExprKind::Binary(op, lhs, rhs) = body.value.kind | ||
&& can_replace_with_contains(op, lhs, rhs) | ||
// iter() | ||
&& let Some((name, recv, _, _, _)) = method_call(recv) | ||
&& name == "iter" | ||
{ | ||
let ref_type = cx.typeck_results().expr_ty_adjusted(recv); | ||
|
||
match ref_type.kind() { | ||
ty::Ref(_, inner_type, _) if inner_type.is_slice() => { | ||
// check if the receiver is a numeric slice | ||
if let ty::Slice(slice_type) = inner_type.kind() | ||
&& ((slice_type.to_string() == "u8" || slice_type.to_string() == "i8") | ||
|| (slice_type.is_numeric() | ||
&& msrv.meets(RustcVersion { | ||
major: 1, | ||
minor: 84, | ||
patch: 0, | ||
}))) | ||
{ | ||
span_lint( | ||
cx, | ||
SLICE_ITER_ANY, | ||
expr.span, | ||
"using `contains()` instead of `iter().any()` is more efficient", | ||
); | ||
} | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} | ||
|
||
fn can_replace_with_contains(op: Spanned<BinOpKind>, lhs: &Expr<'_>, rhs: &Expr<'_>) -> bool { | ||
matches!( | ||
(op.node, &lhs.kind, &rhs.kind), | ||
( | ||
BinOpKind::Eq, | ||
ExprKind::Path(_) | ExprKind::Unary(_, _), | ||
ExprKind::Lit(_) | ExprKind::Path(_) | ||
) | ( | ||
BinOpKind::Eq, | ||
ExprKind::Lit(_), | ||
ExprKind::Path(_) | ExprKind::Unary(_, _) | ||
) | ||
) | ||
} | ||
This file contains 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 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 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 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 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 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 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
Oops, something went wrong.
Oops, something went wrong.
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.
For example, the following code uses
==
inside closures, but cannot simply be replaced bycontains()
:Therefore, this function exclude such cases.