@@ -5,6 +5,7 @@ use rustc_ast::BinOpKind;
55use rustc_errors:: Applicability ;
66use rustc_hir:: { Block , Expr , ExprKind , StmtKind } ;
77use rustc_lint:: { LateContext , LateLintPass } ;
8+ use rustc_middle:: ty:: TyCtxt ;
89use rustc_session:: impl_lint_pass;
910use rustc_span:: Span ;
1011
@@ -77,12 +78,14 @@ declare_clippy_lint! {
7778}
7879
7980pub struct CollapsibleIf {
81+ let_chains_enabled : bool ,
8082 lint_commented_code : bool ,
8183}
8284
8385impl CollapsibleIf {
84- pub fn new ( conf : & ' static Conf ) -> Self {
86+ pub fn new ( tcx : TyCtxt < ' _ > , conf : & ' static Conf ) -> Self {
8587 Self {
88+ let_chains_enabled : tcx. features ( ) . let_chains ( ) ,
8689 lint_commented_code : conf. lint_commented_code ,
8790 }
8891 }
@@ -124,8 +127,7 @@ impl CollapsibleIf {
124127 if let Some ( inner) = expr_block ( then)
125128 && cx. tcx . hir_attrs ( inner. hir_id ) . is_empty ( )
126129 && let ExprKind :: If ( check_inner, _, None ) = & inner. kind
127- // Prevent triggering on `if c { if let a = b { .. } }`.
128- && !matches ! ( check_inner. kind, ExprKind :: Let ( ..) )
130+ && self . eligible_condition ( check_inner)
129131 && let ctxt = expr. span . ctxt ( )
130132 && inner. span . ctxt ( ) == ctxt
131133 && ( self . lint_commented_code || !block_starts_with_comment ( cx, then) )
@@ -160,6 +162,10 @@ impl CollapsibleIf {
160162 ) ;
161163 }
162164 }
165+
166+ pub fn eligible_condition ( & self , cond : & Expr < ' _ > ) -> bool {
167+ self . let_chains_enabled || !matches ! ( cond. kind, ExprKind :: Let ( ..) )
168+ }
163169}
164170
165171impl_lint_pass ! ( CollapsibleIf => [ COLLAPSIBLE_IF , COLLAPSIBLE_ELSE_IF ] ) ;
@@ -174,11 +180,10 @@ impl LateLintPass<'_> for CollapsibleIf {
174180 {
175181 Self :: check_collapsible_else_if ( cx, then. span , else_) ;
176182 } else if else_. is_none ( )
177- && ! matches ! ( cond . kind , ExprKind :: Let ( .. ) )
183+ && self . eligible_condition ( cond )
178184 && let ExprKind :: Block ( then, None ) = then. kind
179185 {
180- // Prevent triggering on `if c { if let a = b { .. } }`.
181- self . check_collapsible_if_if ( cx, expr, cond, block ! ( then) ) ;
186+ self . check_collapsible_if_if ( cx, expr, cond, then) ;
182187 }
183188 }
184189 }
0 commit comments