]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/search_is_some.rs
Fix clippy build
[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::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;
8 use rustc_hir as hir;
9 use rustc_hir::PatKind;
10 use rustc_lint::LateContext;
11 use rustc_middle::ty;
12 use rustc_span::source_map::Span;
13 use rustc_span::symbol::sym;
14
15 use super::SEARCH_IS_SOME;
16
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>(
21     cx: &LateContext<'_>,
22     expr: &'tcx hir::Expr<'_>,
23     search_method: &str,
24     is_some: bool,
25     search_recv: &hir::Expr<'_>,
26     search_arg: &'tcx hir::Expr<'_>,
27     is_some_recv: &hir::Expr<'_>,
28     method_span: Span,
29 ) {
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!(
34             "called `{}()` after searching an `Iterator` with `{}`",
35             option_check_method, search_method
36         );
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);
47                 then {
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)
56                         } else {
57                             Some(search_snippet.to_string())
58                         }
59                     } else {
60                         None
61                     }
62                 } else {
63                     None
64                 }
65             };
66             // add note if not multi-line
67             if is_some {
68                 span_lint_and_sugg(
69                     cx,
70                     SEARCH_IS_SOME,
71                     method_span.with_hi(expr.span.hi()),
72                     &msg,
73                     "use `any()` instead",
74                     format!(
75                         "any({})",
76                         any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
77                     ),
78                     applicability,
79                 );
80             } else {
81                 let iter = snippet(cx, search_recv.span, "..");
82                 span_lint_and_sugg(
83                     cx,
84                     SEARCH_IS_SOME,
85                     expr.span,
86                     &msg,
87                     "use `!_.any()` instead",
88                     format!(
89                         "!{}.any({})",
90                         iter,
91                         any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
92                     ),
93                     applicability,
94                 );
95             }
96         } else {
97             let hint = format!(
98                 "this is more succinctly expressed by calling `any()`{}",
99                 if option_check_method == "is_none" {
100                     " with negation"
101                 } else {
102                     ""
103                 }
104             );
105             span_lint_and_help(cx, SEARCH_IS_SOME, expr.span, &msg, None, &hint);
106         }
107     }
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) {
113                 true
114             } else {
115                 *self_ty.kind() == ty::Str
116             }
117         };
118         if_chain! {
119             if is_string_or_str_slice(search_recv);
120             if is_string_or_str_slice(search_arg);
121             then {
122                 let msg = format!("called `{}()` after calling `find()` on a string", option_check_method);
123                 match option_check_method {
124                     "is_some" => {
125                         let mut applicability = Applicability::MachineApplicable;
126                         let find_arg = snippet_with_applicability(cx, search_arg.span, "..", &mut applicability);
127                         span_lint_and_sugg(
128                             cx,
129                             SEARCH_IS_SOME,
130                             method_span.with_hi(expr.span.hi()),
131                             &msg,
132                             "use `contains()` instead",
133                             format!("contains({})", find_arg),
134                             applicability,
135                         );
136                     },
137                     "is_none" => {
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);
141                         span_lint_and_sugg(
142                             cx,
143                             SEARCH_IS_SOME,
144                             expr.span,
145                             &msg,
146                             "use `!_.contains()` instead",
147                             format!("!{}.contains({})", string, find_arg),
148                             applicability,
149                         );
150                     },
151                     _ => (),
152                 }
153             }
154         }
155     }
156 }