1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
+ use clippy_utils:: higher;
2
3
use clippy_utils:: method_chain_args;
3
4
use clippy_utils:: source:: snippet_with_applicability;
4
5
use clippy_utils:: ty:: is_type_diagnostic_item;
5
6
use if_chain:: if_chain;
6
7
use rustc_errors:: Applicability ;
7
- use rustc_hir:: { Expr , ExprKind , MatchSource , PatKind , QPath } ;
8
+ use rustc_hir:: { Expr , ExprKind , PatKind , QPath } ;
8
9
use rustc_lint:: { LateContext , LateLintPass } ;
9
10
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
10
11
use rustc_span:: sym;
@@ -47,21 +48,21 @@ declare_lint_pass!(MatchResultOk => [MATCH_RESULT_OK]);
47
48
impl < ' tcx > LateLintPass < ' tcx > for MatchResultOk {
48
49
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
49
50
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) ;
56
57
if rustc_hir_pretty:: to_string( rustc_hir_pretty:: NO_ANN , |s| s. print_path( x, false ) ) == "Some" ;
57
58
58
59
then {
59
60
60
61
let mut applicability = Applicability :: MachineApplicable ;
61
62
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) ;
63
64
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) {
65
66
let sugg = format!(
66
67
"if let Ok({}) = {}" ,
67
68
some_expr_string,
@@ -70,13 +71,13 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
70
71
span_lint_and_sugg(
71
72
cx,
72
73
MATCH_RESULT_OK ,
73
- expr. span. with_hi( op . span. hi( ) ) ,
74
+ expr. span. with_hi( let_expr . span. hi( ) ) ,
74
75
"matching on `Some` with `ok()` is redundant" ,
75
76
& format!( "consider matching on `Ok({})` and removing the call to `ok` instead" , some_expr_string) ,
76
77
sugg,
77
78
applicability,
78
79
) ;
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) | {
80
81
let sugg = format!(
81
82
"while let Ok({}) = {}" ,
82
83
some_expr_string,
@@ -85,7 +86,7 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
85
86
span_lint_and_sugg(
86
87
cx,
87
88
MATCH_RESULT_OK ,
88
- expr. span. with_hi( op . span. hi( ) ) ,
89
+ expr. span. with_hi( let_expr . span. hi( ) ) ,
89
90
"matching on `Some` with `ok()` is redundant" ,
90
91
& format!( "consider matching on `Ok({})` and removing the call to `ok` instead" , some_expr_string) ,
91
92
sugg,
0 commit comments