@@ -3,6 +3,7 @@ use std::ops::ControlFlow;
3
3
use clippy_utils:: diagnostics:: span_lint_and_sugg;
4
4
use clippy_utils:: eager_or_lazy:: switch_to_lazy_eval;
5
5
use clippy_utils:: higher:: VecArgs ;
6
+ use clippy_utils:: msrvs:: { self , Msrv } ;
6
7
use clippy_utils:: source:: snippet_with_context;
7
8
use clippy_utils:: ty:: { expr_type_is_certain, implements_trait, is_type_diagnostic_item} ;
8
9
use clippy_utils:: visitors:: for_each_expr;
@@ -25,6 +26,7 @@ pub(super) fn check<'tcx>(
25
26
name : Symbol ,
26
27
receiver : & ' tcx hir:: Expr < ' _ > ,
27
28
args : & ' tcx [ hir:: Expr < ' _ > ] ,
29
+ msrv : Msrv ,
28
30
) {
29
31
if let [ arg] = args {
30
32
let inner_arg = peel_blocks ( arg) ;
@@ -45,11 +47,11 @@ pub(super) fn check<'tcx>(
45
47
} ;
46
48
( !inner_fun_has_args
47
49
&& !is_nested_expr
48
- && check_unwrap_or_default ( cx, name, receiver, fun, Some ( ex) , expr. span , method_span) )
50
+ && check_unwrap_or_default ( cx, name, receiver, fun, Some ( ex) , expr. span , method_span, msrv ) )
49
51
|| check_or_fn_call ( cx, name, method_span, receiver, arg, None , expr. span , fun_span)
50
52
} ,
51
53
hir:: ExprKind :: Path ( ..) | hir:: ExprKind :: Closure ( ..) if !is_nested_expr => {
52
- check_unwrap_or_default ( cx, name, receiver, ex, None , expr. span , method_span)
54
+ check_unwrap_or_default ( cx, name, receiver, ex, None , expr. span , method_span, msrv )
53
55
} ,
54
56
hir:: ExprKind :: Index ( ..) | hir:: ExprKind :: MethodCall ( ..) => {
55
57
check_or_fn_call ( cx, name, method_span, receiver, arg, None , expr. span , None )
@@ -97,6 +99,7 @@ pub(super) fn check<'tcx>(
97
99
/// `or_insert(T::new())` or `or_insert(T::default())`.
98
100
/// Similarly checks for `unwrap_or_else(T::new)`, `unwrap_or_else(T::default)`,
99
101
/// `or_insert_with(T::new)` or `or_insert_with(T::default)`.
102
+ #[ expect( clippy:: too_many_arguments) ]
100
103
fn check_unwrap_or_default (
101
104
cx : & LateContext < ' _ > ,
102
105
name : Symbol ,
@@ -105,7 +108,15 @@ fn check_unwrap_or_default(
105
108
call_expr : Option < & hir:: Expr < ' _ > > ,
106
109
span : Span ,
107
110
method_span : Span ,
111
+ msrv : Msrv ,
108
112
) -> bool {
113
+ let receiver_ty = cx. typeck_results ( ) . expr_ty_adjusted ( receiver) . peel_refs ( ) ;
114
+
115
+ // Check MSRV, but only for `Result::unwrap_or_default`
116
+ if is_type_diagnostic_item ( cx, receiver_ty, sym:: Result ) && !msrv. meets ( cx, msrvs:: RESULT_UNWRAP_OR_DEFAULT ) {
117
+ return false ;
118
+ }
119
+
109
120
if !expr_type_is_certain ( cx, receiver) {
110
121
return false ;
111
122
}
@@ -137,7 +148,6 @@ fn check_unwrap_or_default(
137
148
_ => return false ,
138
149
} ;
139
150
140
- let receiver_ty = cx. typeck_results ( ) . expr_ty_adjusted ( receiver) . peel_refs ( ) ;
141
151
let Some ( suggested_method_def_id) = receiver_ty. ty_adt_def ( ) . and_then ( |adt_def| {
142
152
cx. tcx
143
153
. inherent_impls ( adt_def. did ( ) )
0 commit comments