@@ -84,18 +84,14 @@ impl EarlyLintPass for CollapsibleIf {
8484}
8585
8686fn check_if ( cx : & EarlyContext < ' _ > , expr : & ast:: Expr ) {
87- match expr. node {
88- ast:: ExprKind :: If ( ref check, ref then, ref else_) => {
89- if let Some ( ref else_) = * else_ {
90- check_collapsible_maybe_if_let ( cx, else_) ;
91- } else {
92- check_collapsible_no_if_let ( cx, expr, check, then) ;
93- }
94- } ,
95- ast:: ExprKind :: IfLet ( _, _, _, Some ( ref else_) ) => {
87+ if let ast:: ExprKind :: If ( check, then, else_) = & expr. node {
88+ if let Some ( else_) = else_ {
9689 check_collapsible_maybe_if_let ( cx, else_) ;
97- } ,
98- _ => ( ) ,
90+ } else if let ast:: ExprKind :: Let ( ..) = check. node {
91+ // Prevent triggering on `if let a = b { if c { .. } }`.
92+ } else {
93+ check_collapsible_no_if_let ( cx, expr, check, then) ;
94+ }
9995 }
10096}
10197
@@ -113,22 +109,18 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
113109 if !block_starts_with_comment( cx, block) ;
114110 if let Some ( else_) = expr_block( block) ;
115111 if !in_macro_or_desugar( else_. span) ;
112+ if let ast:: ExprKind :: If ( ..) = else_. node;
116113 then {
117- match else_. node {
118- ast:: ExprKind :: If ( ..) | ast:: ExprKind :: IfLet ( ..) => {
119- let mut applicability = Applicability :: MachineApplicable ;
120- span_lint_and_sugg(
121- cx,
122- COLLAPSIBLE_IF ,
123- block. span,
124- "this `else { if .. }` block can be collapsed" ,
125- "try" ,
126- snippet_block_with_applicability( cx, else_. span, ".." , & mut applicability) . into_owned( ) ,
127- applicability,
128- ) ;
129- }
130- _ => ( ) ,
131- }
114+ let mut applicability = Applicability :: MachineApplicable ;
115+ span_lint_and_sugg(
116+ cx,
117+ COLLAPSIBLE_IF ,
118+ block. span,
119+ "this `else { if .. }` block can be collapsed" ,
120+ "try" ,
121+ snippet_block_with_applicability( cx, else_. span, ".." , & mut applicability) . into_owned( ) ,
122+ applicability,
123+ ) ;
132124 }
133125 }
134126}
@@ -139,6 +131,11 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: &
139131 if let Some ( inner) = expr_block( then) ;
140132 if let ast:: ExprKind :: If ( ref check_inner, ref content, None ) = inner. node;
141133 then {
134+ if let ast:: ExprKind :: Let ( ..) = check_inner. node {
135+ // Prevent triggering on `if c { if let a = b { .. } }`.
136+ return ;
137+ }
138+
142139 if expr. span. ctxt( ) != inner. span. ctxt( ) {
143140 return ;
144141 }
0 commit comments