1+ use clippy_utils:: diagnostics:: span_lint_and_sugg;
12use 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 ;
35use rustc_hir:: * ;
46use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
7+ use rustc_middle:: lint:: in_external_macro;
58use 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;
99use std:: ops:: ControlFlow ;
1010
1111declare_clippy_lint ! {
@@ -47,7 +47,7 @@ declare_clippy_lint! {
4747declare_lint_pass ! ( StackedIfMatch => [ STACKED_IF_MATCH ] ) ;
4848
4949impl < ' 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 < ' _ > ) {
5151 if expr. span . from_expansion ( ) || in_external_macro ( cx. sess ( ) , expr. span ) {
5252 return ;
5353 }
@@ -74,15 +74,20 @@ impl<'tcx> LateLintPass<'tcx> for StackedIfMatch {
7474 return ControlFlow :: Continue ( Descend :: No ) ;
7575 }
7676
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 {
7984 let inner_snippet = snippet ( cx, sub_expr. span , ".." ) ;
8085 span_lint_and_sugg (
8186 cx,
8287 STACKED_IF_MATCH ,
8388 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" ) ,
8691 format ! ( "let result = {inner_snippet}; {keyword} result" ) ,
8792 Applicability :: MachineApplicable ,
8893 ) ;
0 commit comments