11use clippy_utils:: as_some_expr;
2- use clippy_utils:: diagnostics:: span_lint_and_sugg ;
2+ use clippy_utils:: diagnostics:: span_lint_and_then ;
33use clippy_utils:: res:: { MaybeDef , MaybeQPath , MaybeResPath } ;
44use clippy_utils:: visitors:: contains_unsafe_block;
55
@@ -9,7 +9,7 @@ use rustc_lint::LateContext;
99use rustc_span:: { SyntaxContext , sym} ;
1010
1111use super :: MANUAL_FILTER ;
12- use super :: manual_utils:: { SomeExpr , check_with} ;
12+ use super :: manual_utils:: { SomeExpr , SuggInfo , check_with} ;
1313
1414// Function called on the <expr> of `[&+]Some((ref | ref mut) x) => <expr>`
1515// Need to check if it's of the form `<expr>=if <cond> {<then_expr>} else {<else_expr>}`
@@ -133,7 +133,15 @@ fn check<'tcx>(
133133 else_pat : Option < & ' tcx Pat < ' _ > > ,
134134 else_body : & ' tcx Expr < ' _ > ,
135135) {
136- if let Some ( sugg_info) = check_with (
136+ if let Some ( SuggInfo {
137+ needs_brackets,
138+ scrutinee_impl_copy,
139+ scrutinee_str,
140+ as_ref_str,
141+ body_str,
142+ app,
143+ requires_coercion,
144+ } ) = check_with (
137145 cx,
138146 expr,
139147 scrutinee,
@@ -143,22 +151,27 @@ fn check<'tcx>(
143151 else_body,
144152 get_cond_expr,
145153 ) {
146- let body_str = add_ampersand_if_copy ( sugg_info . body_str , sugg_info . scrutinee_impl_copy ) ;
147- span_lint_and_sugg (
154+ let body_str = add_ampersand_if_copy ( body_str, scrutinee_impl_copy) ;
155+ span_lint_and_then (
148156 cx,
149157 MANUAL_FILTER ,
150158 expr. span ,
151159 "manual implementation of `Option::filter`" ,
152- "try" ,
153- if sugg_info. needs_brackets {
154- format ! (
155- "{{ {}{}.filter({body_str}) }}" ,
156- sugg_info. scrutinee_str, sugg_info. as_ref_str
157- )
158- } else {
159- format ! ( "{}{}.filter({body_str})" , sugg_info. scrutinee_str, sugg_info. as_ref_str)
160+ |diag| {
161+ diag. span_suggestion (
162+ expr. span ,
163+ "try" ,
164+ if needs_brackets {
165+ format ! ( "{{ {scrutinee_str}{as_ref_str}.filter({body_str}) }}" )
166+ } else {
167+ format ! ( "{scrutinee_str}{as_ref_str}.filter({body_str})" )
168+ } ,
169+ app,
170+ ) ;
171+ if requires_coercion {
172+ diag. note ( "you may need to add explicit `as` casts/dereferences if this `match` is coerced to another type" ) ;
173+ }
160174 } ,
161- sugg_info. app ,
162175 ) ;
163176 }
164177}
0 commit comments