@@ -11,7 +11,6 @@ use rustc_hir::{Arm, Expr, ExprKind, PatKind};
1111use rustc_lint:: LintContext ;
1212use rustc_lint:: { LateContext , LateLintPass } ;
1313use rustc_middle:: lint:: in_external_macro;
14- use rustc_middle:: ty:: adjustment:: Adjust ;
1514use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1615use rustc_span:: sym;
1716
@@ -54,21 +53,6 @@ impl LateLintPass<'_> for ManualUnwrapOr {
5453 }
5554}
5655
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-
7256fn lint_manual_unwrap_or < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
7357 fn applicable_or_arm < ' a > ( cx : & LateContext < ' _ > , arms : & ' a [ Arm < ' a > ] ) -> Option < & ' a Arm < ' a > > {
7458 if_chain ! {
@@ -87,9 +71,8 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
8771 if is_lang_ctor( cx, qpath, OptionSome ) || is_lang_ctor( cx, qpath, ResultOk ) ;
8872 if let PatKind :: Binding ( _, binding_hir_id, ..) = unwrap_pat. kind;
8973 if path_to_local_id( unwrap_arm. body, binding_hir_id) ;
74+ if cx. typeck_results( ) . expr_adjustments( unwrap_arm. body) . is_empty( ) ;
9075 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 ( ..) ) ) ) ;
9376 then {
9477 Some ( or_arm)
9578 } else {
@@ -101,10 +84,10 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
10184 if_chain ! {
10285 if let ExprKind :: Match ( scrutinee, match_arms, _) = expr. kind;
10386 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" )
10689 } else if is_type_diagnostic_item( cx, ty, sym:: result_type) {
107- Some ( Case :: Result )
90+ Some ( " Result" )
10891 } else {
10992 None
11093 } ;
@@ -127,7 +110,7 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
127110 span_lint_and_sugg(
128111 cx,
129112 MANUAL_UNWRAP_OR , expr. span,
130- & format!( "this pattern reimplements `{}`" , case . unwrap_fn_path ( ) ) ,
113+ & format!( "this pattern reimplements `{}::unwrap_or `" , ty_name ) ,
131114 "replace with" ,
132115 format!(
133116 "{}.unwrap_or({})" ,
0 commit comments