1
+ use clippy_utils:: diagnostics:: span_lint_and_sugg;
1
2
use clippy_utils:: source:: snippet;
2
- use rustc_middle:: lint:: in_external_macro;
3
+ use clippy_utils:: visitors:: { for_each_expr, Descend } ;
4
+ use rustc_errors:: Applicability ;
3
5
use rustc_hir:: * ;
4
6
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
7
+ use rustc_middle:: lint:: in_external_macro;
5
8
use rustc_session:: declare_lint_pass;
6
- use rustc_errors:: Applicability ;
7
- use clippy_utils:: visitors:: { for_each_expr, Descend } ;
8
- use clippy_utils:: diagnostics:: span_lint_and_sugg;
9
9
use std:: ops:: ControlFlow ;
10
10
11
11
declare_clippy_lint ! {
@@ -47,7 +47,7 @@ declare_clippy_lint! {
47
47
declare_lint_pass ! ( StackedIfMatch => [ STACKED_IF_MATCH ] ) ;
48
48
49
49
impl < ' tcx > LateLintPass < ' tcx > for StackedIfMatch {
50
- fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
50
+ fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
51
51
if expr. span . from_expansion ( ) || in_external_macro ( cx. sess ( ) , expr. span ) {
52
52
return ;
53
53
}
@@ -74,15 +74,20 @@ impl<'tcx> LateLintPass<'tcx> for StackedIfMatch {
74
74
return ControlFlow :: Continue ( Descend :: No ) ;
75
75
}
76
76
77
- if ( keyword == "if" && matches ! ( sub_expr. kind, ExprKind :: If ( ..) ) )
78
- || ( keyword == "match" && matches ! ( sub_expr. kind, ExprKind :: Match ( .., MatchSource :: Normal ) ) ) {
77
+ let sub_keyword = match sub_expr. kind {
78
+ ExprKind :: If ( ..) => "if" ,
79
+ ExprKind :: Match ( .., MatchSource :: Normal ) => "match" ,
80
+ _ => "" ,
81
+ } ;
82
+
83
+ if keyword == sub_keyword {
79
84
let inner_snippet = snippet ( cx, sub_expr. span , ".." ) ;
80
85
span_lint_and_sugg (
81
86
cx,
82
87
STACKED_IF_MATCH ,
83
88
expr. span . with_hi ( sub_expr. span . hi ( ) ) ,
84
- format ! ( "avoid using `{keyword} {keyword}`" ) ,
85
- format ! ( "try binding inner `{keyword}` with `let` " ) ,
89
+ format ! ( "avoid using `{keyword} {keyword}` by binding inner `{keyword}` with `let` " ) ,
90
+ format ! ( "try" ) ,
86
91
format ! ( "let result = {inner_snippet}; {keyword} result" ) ,
87
92
Applicability :: MachineApplicable ,
88
93
) ;
0 commit comments