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) {
34 "called `{}()` after searching an `Iterator` with `{}`",
35 option_check_method, search_method
37 let search_snippet = snippet(cx, search_arg.span, "..");
38 if search_snippet.lines().count() <= 1 {
39 // suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
40 // suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
41 let mut applicability = Applicability::MachineApplicable;
42 let any_search_snippet = if_chain! {
43 if search_method == "find";
44 if let hir::ExprKind::Closure(&hir::Closure { body, .. }) = search_arg.kind;
45 let closure_body = cx.tcx.hir().body(body);
46 if let Some(closure_arg) = closure_body.params.get(0);
48 if let hir::PatKind::Ref(..) = closure_arg.pat.kind {
49 Some(search_snippet.replacen('&', "", 1))
50 } else if let PatKind::Binding(..) = strip_pat_refs(closure_arg.pat).kind {
51 // `find()` provides a reference to the item, but `any` does not,
52 // so we should fix item usages for suggestion
53 if let Some(closure_sugg) = deref_closure_args(cx, search_arg) {
54 applicability = closure_sugg.applicability;
55 Some(closure_sugg.suggestion)
57 Some(search_snippet.to_string())
66 // add note if not multi-line
71 method_span.with_hi(expr.span.hi()),
73 "use `any()` instead",
76 any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
81 let iter = snippet(cx, search_recv.span, "..");
87 "use `!_.any()` instead",
91 any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
98 "this is more succinctly expressed by calling `any()`{}",
99 if option_check_method == "is_none" {
105 span_lint_and_help(cx, SEARCH_IS_SOME, expr.span, &msg, None, &hint);
108 // lint if `find()` is called by `String` or `&str`
109 else if search_method == "find" {
110 let is_string_or_str_slice = |e| {
111 let self_ty = cx.typeck_results().expr_ty(e).peel_refs();
112 if is_type_diagnostic_item(cx, self_ty, sym::String) {
115 *self_ty.kind() == ty::Str
119 if is_string_or_str_slice(search_recv);
120 if is_string_or_str_slice(search_arg);
122 let msg = format!("called `{}()` after calling `find()` on a string", option_check_method);
123 match option_check_method {
125 let mut applicability = Applicability::MachineApplicable;
126 let find_arg = snippet_with_applicability(cx, search_arg.span, "..", &mut applicability);
130 method_span.with_hi(expr.span.hi()),
132 "use `contains()` instead",
133 format!("contains({})", find_arg),
138 let string = snippet(cx, search_recv.span, "..");
139 let mut applicability = Applicability::MachineApplicable;
140 let find_arg = snippet_with_applicability(cx, search_arg.span, "..", &mut applicability);
146 "use `!_.contains()` instead",
147 format!("!{}.contains({})", string, find_arg),