@@ -11,7 +11,6 @@ use rustc_hir::{Arm, Expr, ExprKind, PatKind};
11
11
use rustc_lint:: LintContext ;
12
12
use rustc_lint:: { LateContext , LateLintPass } ;
13
13
use rustc_middle:: lint:: in_external_macro;
14
- use rustc_middle:: ty:: adjustment:: Adjust ;
15
14
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
16
15
use rustc_span:: sym;
17
16
@@ -54,21 +53,6 @@ impl LateLintPass<'_> for ManualUnwrapOr {
54
53
}
55
54
}
56
55
57
- #[ derive( Copy , Clone ) ]
58
- enum Case {
59
- Option ,
60
- Result ,
61
- }
62
-
63
- impl Case {
64
- fn unwrap_fn_path ( & self ) -> & str {
65
- match self {
66
- Case :: Option => "Option::unwrap_or" ,
67
- Case :: Result => "Result::unwrap_or" ,
68
- }
69
- }
70
- }
71
-
72
56
fn lint_manual_unwrap_or < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
73
57
fn applicable_or_arm < ' a > ( cx : & LateContext < ' _ > , arms : & ' a [ Arm < ' a > ] ) -> Option < & ' a Arm < ' a > > {
74
58
if_chain ! {
@@ -87,9 +71,8 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
87
71
if is_lang_ctor( cx, qpath, OptionSome ) || is_lang_ctor( cx, qpath, ResultOk ) ;
88
72
if let PatKind :: Binding ( _, binding_hir_id, ..) = unwrap_pat. kind;
89
73
if path_to_local_id( unwrap_arm. body, binding_hir_id) ;
74
+ if cx. typeck_results( ) . expr_adjustments( unwrap_arm. body) . is_empty( ) ;
90
75
if !contains_return_break_continue_macro( or_arm. body) ;
91
- if !cx. typeck_results( ) . expr_adjustments( unwrap_arm. body) . iter( )
92
- . any( |a| matches!( a. kind, Adjust :: Deref ( Some ( ..) ) ) ) ;
93
76
then {
94
77
Some ( or_arm)
95
78
} else {
@@ -101,10 +84,10 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
101
84
if_chain ! {
102
85
if let ExprKind :: Match ( scrutinee, match_arms, _) = expr. kind;
103
86
let ty = cx. typeck_results( ) . expr_ty( scrutinee) ;
104
- if let Some ( case ) = if is_type_diagnostic_item( cx, ty, sym:: option_type) {
105
- Some ( Case :: Option )
87
+ if let Some ( ty_name ) = if is_type_diagnostic_item( cx, ty, sym:: option_type) {
88
+ Some ( " Option" )
106
89
} else if is_type_diagnostic_item( cx, ty, sym:: result_type) {
107
- Some ( Case :: Result )
90
+ Some ( " Result" )
108
91
} else {
109
92
None
110
93
} ;
@@ -127,7 +110,7 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
127
110
span_lint_and_sugg(
128
111
cx,
129
112
MANUAL_UNWRAP_OR , expr. span,
130
- & format!( "this pattern reimplements `{}`" , case . unwrap_fn_path ( ) ) ,
113
+ & format!( "this pattern reimplements `{}::unwrap_or `" , ty_name ) ,
131
114
"replace with" ,
132
115
format!(
133
116
"{}.unwrap_or({})" ,
0 commit comments