@@ -5,25 +5,24 @@ use clippy_utils::source::snippet_with_applicability;
5
5
use clippy_utils:: sugg:: Sugg ;
6
6
use clippy_utils:: { get_parent_expr, sym} ;
7
7
use rustc_errors:: Applicability ;
8
- use rustc_hir as hir;
9
- use rustc_hir:: ExprKind ;
8
+ use rustc_hir:: { Expr , ExprKind } ;
10
9
use rustc_lint:: LateContext ;
11
10
use rustc_span:: Symbol ;
12
11
12
+ #[ expect( clippy:: needless_pass_by_value) ]
13
13
pub ( super ) fn check < ' tcx > (
14
14
cx : & LateContext < ' tcx > ,
15
- expr : & ' tcx hir:: Expr < ' _ > ,
16
- then_recv : & ' tcx hir:: Expr < ' _ > ,
17
- then_arg : & ' tcx hir:: Expr < ' _ > ,
18
- unwrap_arg : Option < & ' tcx hir:: Expr < ' _ > > ,
15
+ expr : & ' tcx Expr < ' _ > ,
16
+ then_recv : & ' tcx Expr < ' _ > ,
17
+ then_arg : & ' tcx Expr < ' _ > ,
19
18
then_method_name : Symbol ,
20
- unwrap_method_name : Symbol ,
19
+ unwrap : Unwrap < ' tcx > ,
21
20
) {
22
21
let recv_ty = cx. typeck_results ( ) . expr_ty ( then_recv) ;
23
22
24
23
if recv_ty. is_bool ( ) {
25
24
let then_eager = switch_to_eager_eval ( cx, then_arg) ;
26
- let unwrap_eager = unwrap_arg . is_none_or ( |arg| switch_to_eager_eval ( cx, arg) ) ;
25
+ let unwrap_eager = unwrap . arg ( ) . is_none_or ( |arg| switch_to_eager_eval ( cx, arg) ) ;
27
26
28
27
let mut applicability = if then_eager && unwrap_eager {
29
28
Applicability :: MachineApplicable
@@ -40,18 +39,17 @@ pub(super) fn check<'tcx>(
40
39
_ => return ,
41
40
} ;
42
41
43
- // FIXME: Add `unwrap_or_else` and `unwrap_or_default` symbol
44
- let els = match unwrap_method_name {
45
- sym :: unwrap_or => snippet_with_applicability ( cx , unwrap_arg . unwrap ( ) . span , ".." , & mut applicability ) ,
46
- sym :: unwrap_or_else if let ExprKind :: Closure ( closure) = unwrap_arg . unwrap ( ) . kind => {
47
- let body = cx. tcx . hir_body ( closure. body ) ;
48
- snippet_with_applicability ( cx, body. value . span , ".." , & mut applicability)
49
- } ,
50
- sym :: unwrap_or_else if let ExprKind :: Path ( _) = unwrap_arg . unwrap ( ) . kind => {
51
- snippet_with_applicability ( cx , unwrap_arg . unwrap ( ) . span , "_" , & mut applicability ) + "()"
42
+ let els = match unwrap {
43
+ Unwrap :: Or ( arg ) => snippet_with_applicability ( cx , arg . span , ".." , & mut applicability ) ,
44
+ Unwrap :: OrElse ( arg ) => match arg . kind {
45
+ ExprKind :: Closure ( closure) => {
46
+ let body = cx. tcx . hir_body ( closure. body ) ;
47
+ snippet_with_applicability ( cx, body. value . span , ".." , & mut applicability)
48
+ } ,
49
+ ExprKind :: Path ( _) => snippet_with_applicability ( cx , arg . span , "_" , & mut applicability ) + "()" ,
50
+ _ => return ,
52
51
} ,
53
- sym:: unwrap_or_default => "Default::default()" . into ( ) ,
54
- _ => return ,
52
+ Unwrap :: OrDefault => "Default::default()" . into ( ) ,
55
53
} ;
56
54
57
55
let sugg = format ! (
@@ -83,3 +81,18 @@ pub(super) fn check<'tcx>(
83
81
) ;
84
82
}
85
83
}
84
+
85
+ pub ( super ) enum Unwrap < ' tcx > {
86
+ Or ( & ' tcx Expr < ' tcx > ) ,
87
+ OrElse ( & ' tcx Expr < ' tcx > ) ,
88
+ OrDefault ,
89
+ }
90
+
91
+ impl < ' tcx > Unwrap < ' tcx > {
92
+ fn arg ( & self ) -> Option < & ' tcx Expr < ' tcx > > {
93
+ match self {
94
+ Self :: Or ( a) | Self :: OrElse ( a) => Some ( a) ,
95
+ Self :: OrDefault => None ,
96
+ }
97
+ }
98
+ }
0 commit comments