-
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.
close #13353 Using `contains()` for slices are more efficient than using `iter().any()`. changelog: [`manual_contains`]: new lint
- Loading branch information
Showing
17 changed files
with
459 additions
and
29 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 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,113 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::eager_or_lazy::switch_to_eager_eval; | ||
use clippy_utils::peel_hir_pat_refs; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::sugg::Sugg; | ||
use rustc_ast::UnOp; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::def::Res; | ||
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, HirId, QPath}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{self}; | ||
use rustc_span::source_map::Spanned; | ||
|
||
use super::MANUAL_CONTAINS; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, closure_arg: &Expr<'_>) { | ||
let mut app = Applicability::MachineApplicable; | ||
|
||
if !expr.span.from_expansion() | ||
// check if `iter().any()` can be replaced with `contains()` | ||
&& let ExprKind::Closure(closure) = closure_arg.kind | ||
&& let Body{params: [param],value} = cx.tcx.hir().body(closure.body) | ||
&& let ExprKind::Binary(op, lhs, rhs) = value.kind | ||
&& let (peeled_ref_pat, _) = peel_hir_pat_refs(param.pat) | ||
&& let Some((snip,snip_expr)) = can_replace_with_contains(cx, op, lhs, rhs, peeled_ref_pat.hir_id, &mut app) | ||
&& let ref_type = cx.typeck_results().expr_ty_adjusted(recv) | ||
&& let ty::Ref(_, inner_type, _) = ref_type.kind() | ||
&& let ty::Slice(slice_type) = inner_type.kind() | ||
&& *slice_type == cx.typeck_results().expr_ty(snip_expr) | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_CONTAINS, | ||
expr.span, | ||
"using `contains()` instead of `iter().any()` is more efficient", | ||
"try", | ||
format!( | ||
"{}.contains({})", | ||
snippet_with_applicability(cx, recv.span, "_", &mut app), | ||
snip | ||
), | ||
app, | ||
); | ||
} | ||
} | ||
|
||
enum EligibleArg { | ||
IsClosureArg, | ||
ContainsArg(String), | ||
} | ||
|
||
fn try_get_eligible_arg<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
closure_arg_id: HirId, | ||
applicability: &mut Applicability, | ||
) -> Option<(EligibleArg, &'tcx Expr<'tcx>)> { | ||
let mut get_snippet = |expr: &Expr<'_>, needs_borrow: bool| { | ||
let sugg = Sugg::hir_with_applicability(cx, expr, "_", applicability); | ||
EligibleArg::ContainsArg((if needs_borrow { sugg.addr() } else { sugg }).to_string()) | ||
}; | ||
|
||
match expr.kind { | ||
ExprKind::Path(QPath::Resolved(_, path)) => { | ||
if path.res == Res::Local(closure_arg_id) { | ||
Some((EligibleArg::IsClosureArg, expr)) | ||
} else { | ||
Some((get_snippet(expr, true), expr)) | ||
} | ||
}, | ||
ExprKind::Unary(UnOp::Deref, inner) => { | ||
if let ExprKind::Path(QPath::Resolved(_, path)) = inner.kind { | ||
if path.res == Res::Local(closure_arg_id) { | ||
Some((EligibleArg::IsClosureArg, expr)) | ||
} else { | ||
Some((get_snippet(inner, false), expr)) | ||
} | ||
} else { | ||
None | ||
} | ||
}, | ||
_ => { | ||
if switch_to_eager_eval(cx, expr) { | ||
Some((get_snippet(expr, true), expr)) | ||
} else { | ||
None | ||
} | ||
}, | ||
} | ||
} | ||
|
||
fn can_replace_with_contains<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
bin_op: Spanned<BinOpKind>, | ||
left_expr: &'tcx Expr<'tcx>, | ||
right_expr: &'tcx Expr<'tcx>, | ||
closure_arg_id: HirId, | ||
applicability: &mut Applicability, | ||
) -> Option<(String, &'tcx Expr<'tcx>)> { | ||
if bin_op.node != BinOpKind::Eq { | ||
return None; | ||
} | ||
|
||
let left_candidate = try_get_eligible_arg(cx, left_expr, closure_arg_id, applicability)?; | ||
let right_candidate = try_get_eligible_arg(cx, right_expr, closure_arg_id, applicability)?; | ||
match (left_candidate, right_candidate) { | ||
((EligibleArg::IsClosureArg, _), (EligibleArg::ContainsArg(snip), candidate_expr)) | ||
| ((EligibleArg::ContainsArg(snip), candidate_expr), (EligibleArg::IsClosureArg, _)) => { | ||
Some((snip, candidate_expr)) | ||
}, | ||
_ => None, | ||
} | ||
} |
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,98 @@ | ||
#![warn(clippy::manual_contains)] | ||
#![allow(clippy::eq_op, clippy::useless_vec)] | ||
|
||
fn should_lint() { | ||
let vec: Vec<u8> = vec![1, 2, 3, 4, 5, 6]; | ||
let values = &vec[..]; | ||
let _ = values.contains(&4); | ||
//~^ manual_contains | ||
|
||
let vec: Vec<u32> = vec![1, 2, 3, 4, 5, 6]; | ||
let values = &vec[..]; | ||
let _ = values.contains(&4); | ||
//~^ manual_contains | ||
|
||
let values: [u8; 6] = [3, 14, 15, 92, 6, 5]; | ||
let _ = values.contains(&10); | ||
//~^ manual_contains | ||
|
||
let num = 14; | ||
let values: [u8; 6] = [3, 14, 15, 92, 6, 5]; | ||
let _ = values.contains(&num); | ||
//~^ manual_contains | ||
|
||
let num = 14; | ||
let values: [u8; 6] = [3, 14, 15, 92, 6, 5]; | ||
let _ = values.contains(&num); | ||
//~^ manual_contains | ||
|
||
let values: [u8; 6] = [3, 14, 15, 92, 6, 5]; | ||
let _ = values.contains(&4); | ||
//~^ manual_contains | ||
|
||
let vec: Vec<u8> = vec![1, 2, 3, 4, 5, 6]; | ||
let values = &vec[..]; | ||
let _ = values.contains(&4); | ||
//~^ manual_contains | ||
|
||
let vec: Vec<u8> = vec![1, 2, 3, 4, 5, 6]; | ||
let values = &vec[..]; | ||
let a = &4; | ||
let _ = values.contains(a); | ||
//~^ manual_contains | ||
|
||
let vec = vec!["1", "2", "3", "4", "5", "6"]; | ||
let values = &vec[..]; | ||
let _ = values.contains(&"4"); | ||
//~^ manual_contains | ||
|
||
let vec: Vec<u32> = vec![1, 2, 3, 4, 5, 6]; | ||
let values = &vec[..]; | ||
let _ = values.contains(&(4 + 1)); | ||
//~^ manual_contains | ||
} | ||
|
||
fn should_not_lint() { | ||
let values: [u8; 6] = [3, 14, 15, 92, 6, 5]; | ||
let _ = values.iter().any(|&v| v > 10); | ||
|
||
let vec: Vec<u32> = vec![1, 2, 3, 4, 5, 6]; | ||
let values = &vec[..]; | ||
let _ = values.iter().any(|&v| v % 2 == 0); | ||
let _ = values.iter().any(|&v| v * 2 == 6); | ||
let _ = values.iter().any(|&v| v == v); | ||
let _ = values.iter().any(|&v| 4 == 4); | ||
let _ = values.contains(&4); | ||
|
||
let a = 1; | ||
let b = 2; | ||
let _ = values.iter().any(|&v| a == b); | ||
let _ = values.iter().any(|&v| a == 4); | ||
|
||
let vec: Vec<String> = vec!["1", "2", "3", "4", "5", "6"] | ||
.iter() | ||
.map(|&x| x.to_string()) | ||
.collect(); | ||
let values = &vec[..]; | ||
let _ = values.iter().any(|v| v == "4"); | ||
|
||
let vec: Vec<u32> = vec![1, 2, 3, 4, 5, 6]; | ||
let values = &vec[..]; | ||
let mut counter = 0; | ||
let mut count = || { | ||
counter += 1; | ||
counter | ||
}; | ||
let _ = values.iter().any(|&v| v == count()); | ||
let _ = values.iter().any(|&v| v == v * 2); | ||
} | ||
|
||
fn foo(values: &[u8]) -> bool { | ||
values.contains(&10) | ||
//~^ manual_contains | ||
} | ||
|
||
fn bar(values: [u8; 3]) -> bool { | ||
values.contains(&10) | ||
//~^ manual_contains | ||
} |
Oops, something went wrong.