@@ -48,8 +48,7 @@ declare_lint_pass!(MatchResultOk => [MATCH_RESULT_OK]);
4848impl < ' tcx > LateLintPass < ' tcx > for MatchResultOk {
4949 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
5050 if_chain ! {
51- if let Some ( higher:: IfLet { let_pat, let_expr, .. } ) = higher:: IfLet :: hir( cx, expr) |
52- if let Some ( higher:: WhileLet { let_pat, let_expr, .. } ) = higher:: WhileLet :: hir( cx, expr) ;
51+ if let Some ( higher:: IfLet { let_pat, let_expr, .. } ) = higher:: IfLet :: hir( cx, expr) ;
5352 if let ExprKind :: MethodCall ( _, ok_span, [ ref result_types_0, ..] , _) = let_expr. kind; //check is expr.ok() has type Result<T,E>.ok(, _)
5453 if let PatKind :: TupleStruct ( QPath :: Resolved ( _, x) , y, _) = let_pat. kind; //get operation
5554 if method_chain_args( let_expr, & [ "ok" ] ) . is_some( ) ; //test to see if using ok() methoduse std::marker::Sized;
@@ -61,38 +60,50 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
6160 let mut applicability = Applicability :: MachineApplicable ;
6261 let some_expr_string = snippet_with_applicability( cx, y[ 0 ] . span, "" , & mut applicability) ;
6362 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+ }
79+
80+ if_chain ! {
81+ if let Some ( higher:: WhileLet { let_pat, let_expr, .. } ) = higher:: WhileLet :: hir( expr) ;
82+ if let ExprKind :: MethodCall ( _, ok_span, [ ref result_types_0, ..] , _) = let_expr. kind; //check is expr.ok() has type Result<T,E>.ok(, _)
83+ if let PatKind :: TupleStruct ( QPath :: Resolved ( _, x) , y, _) = let_pat. kind; //get operation
84+ if method_chain_args( let_expr, & [ "ok" ] ) . is_some( ) ; //test to see if using ok() methoduse std::marker::Sized;
85+ if is_type_diagnostic_item( cx, cx. typeck_results( ) . expr_ty( result_types_0) , sym:: result_type) ;
86+ if rustc_hir_pretty:: to_string( rustc_hir_pretty:: NO_ANN , |s| s. print_path( x, false ) ) == "Some" ;
87+
88+ then {
6489
65- if let Some ( higher:: IfLet { let_pat, let_expr, .. } ) = higher:: IfLet :: hir( cx, expr) {
66- let sugg = format!(
67- "if let Ok({}) = {}" ,
68- some_expr_string,
69- trimmed_ok. trim( ) . trim_end_matches( '.' ) ,
70- ) ;
71- span_lint_and_sugg(
72- cx,
73- MATCH_RESULT_OK ,
74- expr. span. with_hi( let_expr. span. hi( ) ) ,
75- "matching on `Some` with `ok()` is redundant" ,
76- & format!( "consider matching on `Ok({})` and removing the call to `ok` instead" , some_expr_string) ,
77- sugg,
78- applicability,
79- ) ;
80- } else if let Some ( higher:: WhileLet { let_pat, let_expr, .. } ) = higher:: WhileLet :: hir( cx, expr) | {
81- let sugg = format!(
82- "while let Ok({}) = {}" ,
83- some_expr_string,
84- trimmed_ok. trim( ) . trim_end_matches( '.' ) ,
85- ) ;
86- span_lint_and_sugg(
87- cx,
88- MATCH_RESULT_OK ,
89- expr. span. with_hi( let_expr. span. hi( ) ) ,
90- "matching on `Some` with `ok()` is redundant" ,
91- & format!( "consider matching on `Ok({})` and removing the call to `ok` instead" , some_expr_string) ,
92- sugg,
93- applicability,
94- ) ;
95- }
90+ let mut applicability = Applicability :: MachineApplicable ;
91+ let some_expr_string = snippet_with_applicability( cx, y[ 0 ] . span, "" , & mut applicability) ;
92+ let trimmed_ok = snippet_with_applicability( cx, let_expr. span. until( ok_span) , "" , & mut applicability) ;
93+ let sugg = format!(
94+ "while let Ok({}) = {}" ,
95+ some_expr_string,
96+ trimmed_ok. trim( ) . trim_end_matches( '.' ) ,
97+ ) ;
98+ span_lint_and_sugg(
99+ cx,
100+ MATCH_RESULT_OK ,
101+ expr. span. with_hi( let_expr. span. hi( ) ) ,
102+ "matching on `Some` with `ok()` is redundant" ,
103+ & format!( "consider matching on `Ok({})` and removing the call to `ok` instead" , some_expr_string) ,
104+ sugg,
105+ applicability,
106+ ) ;
96107 }
97108 }
98109 }
0 commit comments