@@ -3,6 +3,7 @@ use std::ops::ControlFlow;
33use clippy_utils:: diagnostics:: span_lint_and_sugg;
44use clippy_utils:: eager_or_lazy:: switch_to_lazy_eval;
55use clippy_utils:: higher:: VecArgs ;
6+ use clippy_utils:: msrvs:: { self , Msrv } ;
67use clippy_utils:: source:: snippet_with_context;
78use clippy_utils:: ty:: { expr_type_is_certain, implements_trait, is_type_diagnostic_item} ;
89use clippy_utils:: visitors:: for_each_expr;
@@ -25,6 +26,7 @@ pub(super) fn check<'tcx>(
2526 name : Symbol ,
2627 receiver : & ' tcx hir:: Expr < ' _ > ,
2728 args : & ' tcx [ hir:: Expr < ' _ > ] ,
29+ msrv : Msrv ,
2830) {
2931 if let [ arg] = args {
3032 let inner_arg = peel_blocks ( arg) ;
@@ -45,11 +47,11 @@ pub(super) fn check<'tcx>(
4547 } ;
4648 ( !inner_fun_has_args
4749 && !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 ) )
4951 || check_or_fn_call ( cx, name, method_span, receiver, arg, None , expr. span , fun_span)
5052 } ,
5153 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 )
5355 } ,
5456 hir:: ExprKind :: Index ( ..) | hir:: ExprKind :: MethodCall ( ..) => {
5557 check_or_fn_call ( cx, name, method_span, receiver, arg, None , expr. span , None )
@@ -97,6 +99,7 @@ pub(super) fn check<'tcx>(
9799/// `or_insert(T::new())` or `or_insert(T::default())`.
98100/// Similarly checks for `unwrap_or_else(T::new)`, `unwrap_or_else(T::default)`,
99101/// `or_insert_with(T::new)` or `or_insert_with(T::default)`.
102+ #[ expect( clippy:: too_many_arguments) ]
100103fn check_unwrap_or_default (
101104 cx : & LateContext < ' _ > ,
102105 name : Symbol ,
@@ -105,7 +108,15 @@ fn check_unwrap_or_default(
105108 call_expr : Option < & hir:: Expr < ' _ > > ,
106109 span : Span ,
107110 method_span : Span ,
111+ msrv : Msrv ,
108112) -> 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+
109120 if !expr_type_is_certain ( cx, receiver) {
110121 return false ;
111122 }
@@ -137,7 +148,6 @@ fn check_unwrap_or_default(
137148 _ => return false ,
138149 } ;
139150
140- let receiver_ty = cx. typeck_results ( ) . expr_ty_adjusted ( receiver) . peel_refs ( ) ;
141151 let Some ( suggested_method_def_id) = receiver_ty. ty_adt_def ( ) . and_then ( |adt_def| {
142152 cx. tcx
143153 . inherent_impls ( adt_def. did ( ) )
0 commit comments