11use clippy_utils:: diagnostics:: span_lint_and_sugg;
2+ use clippy_utils:: higher;
23use clippy_utils:: method_chain_args;
34use clippy_utils:: source:: snippet_with_applicability;
45use clippy_utils:: ty:: is_type_diagnostic_item;
56use if_chain:: if_chain;
67use rustc_errors:: Applicability ;
7- use rustc_hir:: { Expr , ExprKind , MatchSource , PatKind , QPath } ;
8+ use rustc_hir:: { Expr , ExprKind , PatKind , QPath } ;
89use rustc_lint:: { LateContext , LateLintPass } ;
910use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1011use rustc_span:: sym;
@@ -47,21 +48,21 @@ declare_lint_pass!(MatchResultOk => [MATCH_RESULT_OK]);
4748impl < ' tcx > LateLintPass < ' tcx > for MatchResultOk {
4849 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
4950 if_chain ! {
50- if let ExprKind :: Match ( op , body , MatchSource :: IfLetDesugar { .. }
51- | MatchSource :: WhileLetDesugar { .. } ) = expr. kind ;
52- if let ExprKind :: MethodCall ( _, ok_span, result_types , _) = op . kind;
53- if let PatKind :: TupleStruct ( QPath :: Resolved ( _, x) , y, _) = body [ 0 ] . pat . kind; // get operation
54- if method_chain_args( op , & [ "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) ;
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) ;
53+ if let ExprKind :: MethodCall ( _, ok_span, [ ref result_types_0 , .. ] , _) = let_expr . kind; //check is expr.ok() has type Result<T,E>.ok(, _)
54+ if let PatKind :: TupleStruct ( QPath :: Resolved ( _, x) , y, _) = let_pat . kind; //get operation
55+ if method_chain_args( let_expr , & [ "ok" ] ) . is_some( ) ; //test to see if using ok() methoduse std::marker::Sized;
56+ if is_type_diagnostic_item( cx, cx. typeck_results( ) . expr_ty( result_types_0 ) , sym:: result_type) ;
5657 if rustc_hir_pretty:: to_string( rustc_hir_pretty:: NO_ANN , |s| s. print_path( x, false ) ) == "Some" ;
5758
5859 then {
5960
6061 let mut applicability = Applicability :: MachineApplicable ;
6162 let some_expr_string = snippet_with_applicability( cx, y[ 0 ] . span, "" , & mut applicability) ;
62- let trimmed_ok = snippet_with_applicability( cx, op . span. until( ok_span) , "" , & mut applicability) ;
63+ let trimmed_ok = snippet_with_applicability( cx, let_expr . span. until( ok_span) , "" , & mut applicability) ;
6364
64- if let ExprKind :: Match ( op , _ , MatchSource :: IfLetDesugar { .. } ) = expr. kind {
65+ if let Some ( higher :: IfLet { let_pat , let_expr , .. } ) = higher :: IfLet :: hir ( cx , expr) {
6566 let sugg = format!(
6667 "if let Ok({}) = {}" ,
6768 some_expr_string,
@@ -70,13 +71,13 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
7071 span_lint_and_sugg(
7172 cx,
7273 MATCH_RESULT_OK ,
73- expr. span. with_hi( op . span. hi( ) ) ,
74+ expr. span. with_hi( let_expr . span. hi( ) ) ,
7475 "matching on `Some` with `ok()` is redundant" ,
7576 & format!( "consider matching on `Ok({})` and removing the call to `ok` instead" , some_expr_string) ,
7677 sugg,
7778 applicability,
7879 ) ;
79- } else if let ExprKind :: Match ( op , _ , MatchSource :: WhileLetDesugar { .. } ) = expr. kind {
80+ } else if let Some ( higher :: WhileLet { let_pat , let_expr , .. } ) = higher :: WhileLet :: hir ( cx , expr) | {
8081 let sugg = format!(
8182 "while let Ok({}) = {}" ,
8283 some_expr_string,
@@ -85,7 +86,7 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
8586 span_lint_and_sugg(
8687 cx,
8788 MATCH_RESULT_OK ,
88- expr. span. with_hi( op . span. hi( ) ) ,
89+ expr. span. with_hi( let_expr . span. hi( ) ) ,
8990 "matching on `Some` with `ok()` is redundant" ,
9091 & format!( "consider matching on `Ok({})` and removing the call to `ok` instead" , some_expr_string) ,
9192 sugg,
0 commit comments