1 use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
2 use clippy_utils::source::{snippet, snippet_with_applicability};
3 use clippy_utils::sugg::deref_closure_args;
4 use clippy_utils::ty::is_type_diagnostic_item;
5 use clippy_utils::{is_trait_method, strip_pat_refs};
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
9 use rustc_hir::PatKind;
10 use rustc_lint::LateContext;
12 use rustc_span::source_map::Span;
13 use rustc_span::symbol::sym;
15 use super::SEARCH_IS_SOME;
17 /// lint searching an Iterator followed by `is_some()`
18 /// or calling `find()` on a string followed by `is_some()` or `is_none()`
19 #[allow(clippy::too_many_arguments, clippy::too_many_lines)]
20 pub(super) fn check<'tcx>(
22 expr: &'tcx hir::Expr<'_>,
25 search_recv: &hir::Expr<'_>,
26 search_arg: &'tcx hir::Expr<'_>,
27 is_some_recv: &hir::Expr<'_>,
30 let option_check_method = if is_some { "is_some" } else { "is_none" };
31 // lint if caller of search is an Iterator
32 if is_trait_method(cx, is_some_recv, sym::Iterator) {
33 let msg = format!("called `{option_check_method}()` after searching an `Iterator` with `{search_method}`");
34 let search_snippet = snippet(cx, search_arg.span, "..");
35 if search_snippet.lines().count() <= 1 {
36 // suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
37 // suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
38 let mut applicability = Applicability::MachineApplicable;
39 let any_search_snippet = if_chain! {
40 if search_method == "find";
41 if let hir::ExprKind::Closure(&hir::Closure { body, .. }) = search_arg.kind;
42 let closure_body = cx.tcx.hir().body(body);
43 if let Some(closure_arg) = closure_body.params.get(0);
45 if let hir::PatKind::Ref(..) = closure_arg.pat.kind {
46 Some(search_snippet.replacen('&', "", 1))
47 } else if let PatKind::Binding(..) = strip_pat_refs(closure_arg.pat).kind {
48 // `find()` provides a reference to the item, but `any` does not,
49 // so we should fix item usages for suggestion
50 if let Some(closure_sugg) = deref_closure_args(cx, search_arg) {
51 applicability = closure_sugg.applicability;
52 Some(closure_sugg.suggestion)
54 Some(search_snippet.to_string())
63 // add note if not multi-line
68 method_span.with_hi(expr.span.hi()),
70 "use `any()` instead",
73 any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
78 let iter = snippet(cx, search_recv.span, "..");
84 "use `!_.any()` instead",
87 any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
94 "this is more succinctly expressed by calling `any()`{}",
95 if option_check_method == "is_none" {
101 span_lint_and_help(cx, SEARCH_IS_SOME, expr.span, &msg, None, &hint);
104 // lint if `find()` is called by `String` or `&str`
105 else if search_method == "find" {
106 let is_string_or_str_slice = |e| {
107 let self_ty = cx.typeck_results().expr_ty(e).peel_refs();
108 if is_type_diagnostic_item(cx, self_ty, sym::String) {
111 *self_ty.kind() == ty::Str
115 if is_string_or_str_slice(search_recv);
116 if is_string_or_str_slice(search_arg);
118 let msg = format!("called `{option_check_method}()` after calling `find()` on a string");
119 match option_check_method {
121 let mut applicability = Applicability::MachineApplicable;
122 let find_arg = snippet_with_applicability(cx, search_arg.span, "..", &mut applicability);
126 method_span.with_hi(expr.span.hi()),
128 "use `contains()` instead",
129 format!("contains({find_arg})"),
134 let string = snippet(cx, search_recv.span, "..");
135 let mut applicability = Applicability::MachineApplicable;
136 let find_arg = snippet_with_applicability(cx, search_arg.span, "..", &mut applicability);
142 "use `!_.contains()` instead",
143 format!("!{string}.contains({find_arg})"),