]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/search_is_some.rs
Move some utils to ty_utils
[rust.git] / clippy_lints / src / methods / search_is_some.rs
1 use crate::utils::{
2     is_trait_method, snippet, snippet_with_applicability, span_lint_and_help, span_lint_and_sugg, strip_pat_refs,
3 };
4 use clippy_utils::ty::is_type_diagnostic_item;
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir as hir;
8 use rustc_hir::PatKind;
9 use rustc_lint::LateContext;
10 use rustc_middle::ty;
11 use rustc_span::source_map::Span;
12 use rustc_span::symbol::sym;
13
14 use super::SEARCH_IS_SOME;
15
16 /// lint searching an Iterator followed by `is_some()`
17 /// or calling `find()` on a string followed by `is_some()`
18 pub(super) fn check<'tcx>(
19     cx: &LateContext<'tcx>,
20     expr: &'tcx hir::Expr<'_>,
21     search_method: &str,
22     search_args: &'tcx [hir::Expr<'_>],
23     is_some_args: &'tcx [hir::Expr<'_>],
24     method_span: Span,
25 ) {
26     // lint if caller of search is an Iterator
27     if is_trait_method(cx, &is_some_args[0], sym::Iterator) {
28         let msg = format!(
29             "called `is_some()` after searching an `Iterator` with `{}`",
30             search_method
31         );
32         let hint = "this is more succinctly expressed by calling `any()`";
33         let search_snippet = snippet(cx, search_args[1].span, "..");
34         if search_snippet.lines().count() <= 1 {
35             // suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
36             // suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
37             let any_search_snippet = if_chain! {
38                 if search_method == "find";
39                 if let hir::ExprKind::Closure(_, _, body_id, ..) = search_args[1].kind;
40                 let closure_body = cx.tcx.hir().body(body_id);
41                 if let Some(closure_arg) = closure_body.params.get(0);
42                 then {
43                     if let hir::PatKind::Ref(..) = closure_arg.pat.kind {
44                         Some(search_snippet.replacen('&', "", 1))
45                     } else if let PatKind::Binding(_, _, ident, _) = strip_pat_refs(&closure_arg.pat).kind {
46                         let name = &*ident.name.as_str();
47                         Some(search_snippet.replace(&format!("*{}", name), name))
48                     } else {
49                         None
50                     }
51                 } else {
52                     None
53                 }
54             };
55             // add note if not multi-line
56             span_lint_and_sugg(
57                 cx,
58                 SEARCH_IS_SOME,
59                 method_span.with_hi(expr.span.hi()),
60                 &msg,
61                 "use `any()` instead",
62                 format!(
63                     "any({})",
64                     any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
65                 ),
66                 Applicability::MachineApplicable,
67             );
68         } else {
69             span_lint_and_help(cx, SEARCH_IS_SOME, expr.span, &msg, None, hint);
70         }
71     }
72     // lint if `find()` is called by `String` or `&str`
73     else if search_method == "find" {
74         let is_string_or_str_slice = |e| {
75             let self_ty = cx.typeck_results().expr_ty(e).peel_refs();
76             if is_type_diagnostic_item(cx, self_ty, sym::string_type) {
77                 true
78             } else {
79                 *self_ty.kind() == ty::Str
80             }
81         };
82         if_chain! {
83             if is_string_or_str_slice(&search_args[0]);
84             if is_string_or_str_slice(&search_args[1]);
85             then {
86                 let msg = "called `is_some()` after calling `find()` on a string";
87                 let mut applicability = Applicability::MachineApplicable;
88                 let find_arg = snippet_with_applicability(cx, search_args[1].span, "..", &mut applicability);
89                 span_lint_and_sugg(
90                     cx,
91                     SEARCH_IS_SOME,
92                     method_span.with_hi(expr.span.hi()),
93                     msg,
94                     "use `contains()` instead",
95                     format!("contains({})", find_arg),
96                     applicability,
97                 );
98             }
99         }
100     }
101 }