@@ -47,38 +47,15 @@ declare_lint_pass!(MatchResultOk => [MATCH_RESULT_OK]);
47
47
48
48
impl < ' tcx > LateLintPass < ' tcx > for MatchResultOk {
49
49
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
50
- if_chain ! {
51
- if let Some ( higher:: IfLet { let_pat, let_expr, .. } ) = higher:: IfLet :: hir( cx, expr) ;
52
- if let ExprKind :: MethodCall ( _, ok_span, [ ref result_types_0, ..] , _) = let_expr. kind; //check is expr.ok() has type Result<T,E>.ok(, _)
53
- if let PatKind :: TupleStruct ( QPath :: Resolved ( _, x) , y, _) = let_pat. kind; //get operation
54
- if method_chain_args( let_expr, & [ "ok" ] ) . is_some( ) ; //test to see if using ok() methoduse std::marker::Sized;
55
- if is_type_diagnostic_item( cx, cx. typeck_results( ) . expr_ty( result_types_0) , sym:: result_type) ;
56
- if rustc_hir_pretty:: to_string( rustc_hir_pretty:: NO_ANN , |s| s. print_path( x, false ) ) == "Some" ;
57
-
58
- then {
59
-
60
- let mut applicability = Applicability :: MachineApplicable ;
61
- let some_expr_string = snippet_with_applicability( cx, y[ 0 ] . span, "" , & mut applicability) ;
62
- let trimmed_ok = snippet_with_applicability( cx, let_expr. span. until( ok_span) , "" , & mut applicability) ;
63
- let sugg = format!(
64
- "if let Ok({}) = {}" ,
65
- some_expr_string,
66
- trimmed_ok. trim( ) . trim_end_matches( '.' ) ,
67
- ) ;
68
- span_lint_and_sugg(
69
- cx,
70
- MATCH_RESULT_OK ,
71
- expr. span. with_hi( let_expr. span. hi( ) ) ,
72
- "matching on `Some` with `ok()` is redundant" ,
73
- & format!( "consider matching on `Ok({})` and removing the call to `ok` instead" , some_expr_string) ,
74
- sugg,
75
- applicability,
76
- ) ;
77
- }
78
- }
50
+ let ( let_pat, let_expr, ifwhile) = if let Some ( higher:: IfLet { let_pat, let_expr, .. } ) = higher:: IfLet :: hir ( cx, expr) {
51
+ ( let_pat, let_expr, "if" )
52
+ } else if let Some ( higher:: WhileLet { let_pat, let_expr, .. } ) = higher:: WhileLet :: hir ( expr) {
53
+ ( let_pat, let_expr, "while" )
54
+ } else {
55
+ return
56
+ } ;
79
57
80
58
if_chain ! {
81
- if let Some ( higher:: WhileLet { let_pat, let_expr, .. } ) = higher:: WhileLet :: hir( expr) ;
82
59
if let ExprKind :: MethodCall ( _, ok_span, [ ref result_types_0, ..] , _) = let_expr. kind; //check is expr.ok() has type Result<T,E>.ok(, _)
83
60
if let PatKind :: TupleStruct ( QPath :: Resolved ( _, x) , y, _) = let_pat. kind; //get operation
84
61
if method_chain_args( let_expr, & [ "ok" ] ) . is_some( ) ; //test to see if using ok() methoduse std::marker::Sized;
@@ -91,7 +68,8 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
91
68
let some_expr_string = snippet_with_applicability( cx, y[ 0 ] . span, "" , & mut applicability) ;
92
69
let trimmed_ok = snippet_with_applicability( cx, let_expr. span. until( ok_span) , "" , & mut applicability) ;
93
70
let sugg = format!(
94
- "while let Ok({}) = {}" ,
71
+ "{} let Ok({}) = {}" ,
72
+ ifwhile,
95
73
some_expr_string,
96
74
trimmed_ok. trim( ) . trim_end_matches( '.' ) ,
97
75
) ;
0 commit comments