]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/search_is_some.rs
Auto merge of #6988 - mikerite:fix-6984, r=camsteffen
[rust.git] / clippy_lints / src / methods / search_is_some.rs
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::ty::is_type_diagnostic_item;
4 use clippy_utils::{is_trait_method, strip_pat_refs};
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()` or `is_none()`
18 #[allow(clippy::too_many_arguments, clippy::too_many_lines)]
19 pub(super) fn check<'tcx>(
20     cx: &LateContext<'_>,
21     expr: &'tcx hir::Expr<'_>,
22     search_method: &str,
23     is_some: bool,
24     search_recv: &hir::Expr<'_>,
25     search_arg: &'tcx hir::Expr<'_>,
26     is_some_recv: &hir::Expr<'_>,
27     method_span: Span,
28 ) {
29     let option_check_method = if is_some { "is_some" } else { "is_none" };
30     // lint if caller of search is an Iterator
31     if is_trait_method(cx, is_some_recv, sym::Iterator) {
32         let msg = format!(
33             "called `{}()` after searching an `Iterator` with `{}`",
34             option_check_method, search_method
35         );
36         let search_snippet = snippet(cx, search_arg.span, "..");
37         if search_snippet.lines().count() <= 1 {
38             // suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
39             // suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
40             let any_search_snippet = if_chain! {
41                 if search_method == "find";
42                 if let hir::ExprKind::Closure(_, _, body_id, ..) = search_arg.kind;
43                 let closure_body = cx.tcx.hir().body(body_id);
44                 if let Some(closure_arg) = closure_body.params.get(0);
45                 then {
46                     if let hir::PatKind::Ref(..) = closure_arg.pat.kind {
47                         Some(search_snippet.replacen('&', "", 1))
48                     } else if let PatKind::Binding(_, _, ident, _) = strip_pat_refs(&closure_arg.pat).kind {
49                         let name = &*ident.name.as_str();
50                         Some(search_snippet.replace(&format!("*{}", name), name))
51                     } else {
52                         None
53                     }
54                 } else {
55                     None
56                 }
57             };
58             // add note if not multi-line
59             if is_some {
60                 span_lint_and_sugg(
61                     cx,
62                     SEARCH_IS_SOME,
63                     method_span.with_hi(expr.span.hi()),
64                     &msg,
65                     "use `any()` instead",
66                     format!(
67                         "any({})",
68                         any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
69                     ),
70                     Applicability::MachineApplicable,
71                 );
72             } else {
73                 let iter = snippet(cx, search_recv.span, "..");
74                 span_lint_and_sugg(
75                     cx,
76                     SEARCH_IS_SOME,
77                     expr.span,
78                     &msg,
79                     "use `!_.any()` instead",
80                     format!(
81                         "!{}.any({})",
82                         iter,
83                         any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
84                     ),
85                     Applicability::MachineApplicable,
86                 );
87             }
88         } else {
89             let hint = format!(
90                 "this is more succinctly expressed by calling `any()`{}",
91                 if option_check_method == "is_none" {
92                     " with negation"
93                 } else {
94                     ""
95                 }
96             );
97             span_lint_and_help(cx, SEARCH_IS_SOME, expr.span, &msg, None, &hint);
98         }
99     }
100     // lint if `find()` is called by `String` or `&str`
101     else if search_method == "find" {
102         let is_string_or_str_slice = |e| {
103             let self_ty = cx.typeck_results().expr_ty(e).peel_refs();
104             if is_type_diagnostic_item(cx, self_ty, sym::string_type) {
105                 true
106             } else {
107                 *self_ty.kind() == ty::Str
108             }
109         };
110         if_chain! {
111             if is_string_or_str_slice(&search_recv);
112             if is_string_or_str_slice(&search_arg);
113             then {
114                 let msg = format!("called `{}()` after calling `find()` on a string", option_check_method);
115                 match option_check_method {
116                     "is_some" => {
117                         let mut applicability = Applicability::MachineApplicable;
118                         let find_arg = snippet_with_applicability(cx, search_arg.span, "..", &mut applicability);
119                         span_lint_and_sugg(
120                             cx,
121                             SEARCH_IS_SOME,
122                             method_span.with_hi(expr.span.hi()),
123                             &msg,
124                             "use `contains()` instead",
125                             format!("contains({})", find_arg),
126                             applicability,
127                         );
128                     },
129                     "is_none" => {
130                         let string = snippet(cx, search_recv.span, "..");
131                         let mut applicability = Applicability::MachineApplicable;
132                         let find_arg = snippet_with_applicability(cx, search_arg.span, "..", &mut applicability);
133                         span_lint_and_sugg(
134                             cx,
135                             SEARCH_IS_SOME,
136                             expr.span,
137                             &msg,
138                             "use `!_.contains()` instead",
139                             format!("!{}.contains({})", string, find_arg),
140                             applicability,
141                         );
142                     },
143                     _ => (),
144                 }
145             }
146         }
147     }
148 }