@@ -2,7 +2,8 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
22use clippy_utils:: source:: { snippet, snippet_with_applicability} ;
33use clippy_utils:: sugg:: deref_closure_args;
44use clippy_utils:: ty:: is_type_lang_item;
5- use clippy_utils:: { is_trait_method, strip_pat_refs} ;
5+ use clippy_utils:: { get_parent_expr, is_trait_method, strip_pat_refs} ;
6+ use hir:: ExprKind ;
67use rustc_errors:: Applicability ;
78use rustc_hir as hir;
89use rustc_hir:: PatKind ;
@@ -72,16 +73,24 @@ pub(super) fn check<'tcx>(
7273) ;
7374} else {
7475let iter = snippet ( cx, search_recv. span , ".." ) ;
76+ let sugg = if is_receiver_of_method_call ( cx, expr) {
77+ format ! (
78+ "(!{iter}.any({}))" ,
79+ any_search_snippet. as_ref( ) . map_or( & * search_snippet, String :: as_str)
80+ )
81+ } else {
82+ format ! (
83+ "!{iter}.any({})" ,
84+ any_search_snippet. as_ref( ) . map_or( & * search_snippet, String :: as_str)
85+ )
86+ } ;
7587span_lint_and_sugg (
7688 cx,
7789SEARCH_IS_SOME ,
7890 expr. span ,
7991 msg,
8092"consider using" ,
81- format ! (
82- "!{iter}.any({})" ,
83- any_search_snippet. as_ref( ) . map_or( & * search_snippet, String :: as_str)
84- ) ,
93+ sugg,
8594 applicability,
8695) ;
8796}
@@ -127,13 +136,18 @@ pub(super) fn check<'tcx>(
127136let string = snippet ( cx, search_recv. span , ".." ) ;
128137let mut applicability = Applicability :: MachineApplicable ;
129138let find_arg = snippet_with_applicability ( cx, search_arg. span , ".." , & mut applicability) ;
139+ let sugg = if is_receiver_of_method_call ( cx, expr) {
140+ format ! ( "(!{string}.contains({find_arg}))" )
141+ } else {
142+ format ! ( "!{string}.contains({find_arg})" )
143+ } ;
130144span_lint_and_sugg (
131145 cx,
132146SEARCH_IS_SOME ,
133147 expr. span ,
134148 msg,
135149"consider using" ,
136- format ! ( "!{string}.contains({find_arg})" ) ,
150+ sugg ,
137151 applicability,
138152) ;
139153} ,
@@ -142,3 +156,12 @@ pub(super) fn check<'tcx>(
142156}
143157}
144158}
159+
160+ fn is_receiver_of_method_call ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > ) -> bool {
161+ if let Some ( parent_expr) = get_parent_expr ( cx, expr)
162+ && let ExprKind :: MethodCall ( ..) = parent_expr. kind
163+ {
164+ return true ;
165+ }
166+ false
167+ }
0 commit comments