-
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.
- Loading branch information
1 parent
c1d5108
commit 86fee9a
Showing
5 changed files
with
66 additions
and
71 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::methods::method_call; | ||
use clippy_utils::diagnostics::span_lint; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::{self, IntTy, UintTy}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of `iter().any()` on slices of `u8` or `i8` and suggests using `contains()` instead. | ||
/// | ||
/// ### Why is this bad? | ||
/// `iter().any()` on slices of `u8` or `i8` is optimized to use `memchr`. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// fn foo(values: &[u8]) -> bool { | ||
/// values.iter().any(|&v| v == 10) | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// fn foo(values: &[u8]) -> bool { | ||
/// values.contains(&10) | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.85.0"] | ||
pub SLICE_ITER_ANY, | ||
perf, | ||
"using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient" | ||
} | ||
|
||
declare_lint_pass!(SliceIterAny => [SLICE_ITER_ANY]); | ||
|
||
impl LateLintPass<'_> for SliceIterAny { | ||
fn check_expr(&mut self, 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" | ||
// check if the receiver is a u8/i8 slice | ||
&& let ty::Ref(_, inner_type, _) = cx.typeck_results().expr_ty(recv).kind() | ||
&& let ty::Slice(slice_type) = inner_type.kind() | ||
&& matches!(slice_type.kind(), ty::Uint(UintTy::U8) | ty::Int(IntTy::I8)) | ||
{ | ||
span_lint( | ||
cx, | ||
SLICE_ITER_ANY, | ||
expr.span, | ||
"using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient", | ||
); | ||
} | ||
} | ||
} |