@@ -9,8 +9,8 @@ use rustc_span::sym;
9
9
use clippy_utils:: diagnostics:: span_lint_and_sugg;
10
10
use clippy_utils:: higher:: IfLetOrMatch ;
11
11
use clippy_utils:: sugg:: Sugg ;
12
- use clippy_utils:: ty:: implements_trait;
13
- use clippy_utils:: { is_default_equivalent, is_in_const_context, peel_blocks, span_contains_comment} ;
12
+ use clippy_utils:: ty:: { expr_type_is_certain , implements_trait} ;
13
+ use clippy_utils:: { is_default_equivalent, is_in_const_context, path_res , peel_blocks, span_contains_comment} ;
14
14
15
15
declare_clippy_lint ! {
16
16
/// ### What it does
@@ -158,6 +158,36 @@ fn handle<'tcx>(cx: &LateContext<'tcx>, if_let_or_match: IfLetOrMatch<'tcx>, exp
158
158
} else {
159
159
Applicability :: MachineApplicable
160
160
} ;
161
+
162
+ // We now check if the condition is a None variant, in which case we need to specify the type
163
+ if path_res ( cx, condition)
164
+ . opt_def_id ( )
165
+ . is_some_and ( |id| Some ( cx. tcx . parent ( id) ) == cx. tcx . lang_items ( ) . option_none_variant ( ) )
166
+ {
167
+ return span_lint_and_sugg (
168
+ cx,
169
+ MANUAL_UNWRAP_OR_DEFAULT ,
170
+ expr. span ,
171
+ format ! ( "{expr_name} can be simplified with `.unwrap_or_default()`" ) ,
172
+ "replace it with" ,
173
+ format ! ( "{receiver}::<{expr_type}>.unwrap_or_default()" ) ,
174
+ applicability,
175
+ ) ;
176
+ }
177
+
178
+ // We check if the expression type is still uncertain, in which case we ask the user to specify it
179
+ if !expr_type_is_certain ( cx, condition) {
180
+ return span_lint_and_sugg (
181
+ cx,
182
+ MANUAL_UNWRAP_OR_DEFAULT ,
183
+ expr. span ,
184
+ format ! ( "{expr_name} can be simplified with `.unwrap_or_default()`" ) ,
185
+ format ! ( "ascribe the type {expr_type} and replace your expression with" ) ,
186
+ format ! ( "{receiver}.unwrap_or_default()" ) ,
187
+ Applicability :: Unspecified ,
188
+ ) ;
189
+ }
190
+
161
191
span_lint_and_sugg (
162
192
cx,
163
193
MANUAL_UNWRAP_OR_DEFAULT ,
0 commit comments