-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lint for checking unnecessary iter().any()
- Loading branch information
1 parent
b3a1693
commit a956f34
Showing
11 changed files
with
144 additions
and
84 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{self}; | ||
|
||
use super::{SLICE_ITER_ANY, UNNECESSARY_ITER_ANY, method_call}; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if !expr.span.from_expansion() | ||
// any() | ||
&& let Some((name, recv, args, _, _)) = method_call(expr) | ||
&& name == "any" | ||
// check if the inner closure is a equality check | ||
&& args.len() == 1 | ||
&& let ExprKind::Closure(closure) = args[0].kind | ||
&& let body = cx.tcx.hir().body(closure.body) | ||
&& let ExprKind::Binary(op, _, _) = body.value.kind | ||
&& op.node == rustc_ast::ast::BinOpKind::Eq | ||
// iter() | ||
&& let Some((name, recv, _, _, _)) = method_call(recv) | ||
&& name == "iter" | ||
{ | ||
let ref_type = cx.typeck_results().expr_ty(recv); | ||
|
||
match ref_type.kind() { | ||
ty::Ref(_, inner_type, _) if inner_type.is_slice() => { | ||
// check if the receiver is a u8/i8 slice | ||
if let ty::Slice(slice_type) = inner_type.kind() | ||
&& (slice_type.to_string() == "u8" || slice_type.to_string() == "i8") | ||
{ | ||
span_lint( | ||
cx, | ||
SLICE_ITER_ANY, | ||
expr.span, | ||
"using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient", | ||
); | ||
} | ||
}, | ||
// if it's an array that uses `iter().any()` and its closure is an equality check, suggest using | ||
// `contains()` (currently only for numeric arrays because of the difficulty in determining whether | ||
// `contains()` can be replaced by `contains()` for arrays of general types) | ||
ty::Array(array_type, _) if array_type.is_numeric() => span_lint( | ||
cx, | ||
UNNECESSARY_ITER_ANY, | ||
expr.span, | ||
"using `contains()` instead of `iter().any()` is more readable", | ||
), | ||
_ => {}, | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,32 @@ | ||
error: using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient | ||
--> tests/ui/iter_any.rs:6:13 | ||
| | ||
LL | let _ = values.iter().any(|&v| v == 4); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::slice-iter-any` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::slice_iter_any)]` | ||
|
||
error: using `contains()` instead of `iter().any()` is more readable | ||
--> tests/ui/iter_any.rs:20:13 | ||
| | ||
LL | let _ = values.iter().any(|&v| v == 10); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::unnecessary-iter-any` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_iter_any)]` | ||
|
||
error: using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient | ||
--> tests/ui/iter_any.rs:29:5 | ||
| | ||
LL | values.iter().any(|&v| v == 10) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: using `contains()` instead of `iter().any()` is more readable | ||
--> tests/ui/iter_any.rs:34:5 | ||
| | ||
LL | values.iter().any(|&v| v == 10) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 4 previous errors | ||
|
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.