@@ -5,6 +5,7 @@ use rustc_ast::BinOpKind;
5
5
use rustc_errors:: Applicability ;
6
6
use rustc_hir:: { Block , Expr , ExprKind , StmtKind } ;
7
7
use rustc_lint:: { LateContext , LateLintPass } ;
8
+ use rustc_middle:: ty:: TyCtxt ;
8
9
use rustc_session:: impl_lint_pass;
9
10
use rustc_span:: Span ;
10
11
@@ -77,12 +78,14 @@ declare_clippy_lint! {
77
78
}
78
79
79
80
pub struct CollapsibleIf {
81
+ let_chains_enabled : bool ,
80
82
lint_commented_code : bool ,
81
83
}
82
84
83
85
impl CollapsibleIf {
84
- pub fn new ( conf : & ' static Conf ) -> Self {
86
+ pub fn new ( tcx : TyCtxt < ' _ > , conf : & ' static Conf ) -> Self {
85
87
Self {
88
+ let_chains_enabled : tcx. features ( ) . let_chains ( ) ,
86
89
lint_commented_code : conf. lint_commented_code ,
87
90
}
88
91
}
@@ -124,8 +127,7 @@ impl CollapsibleIf {
124
127
if let Some ( inner) = expr_block ( then)
125
128
&& cx. tcx . hir_attrs ( inner. hir_id ) . is_empty ( )
126
129
&& 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)
129
131
&& let ctxt = expr. span . ctxt ( )
130
132
&& inner. span . ctxt ( ) == ctxt
131
133
&& ( self . lint_commented_code || !block_starts_with_comment ( cx, then) )
@@ -160,6 +162,10 @@ impl CollapsibleIf {
160
162
) ;
161
163
}
162
164
}
165
+
166
+ pub fn eligible_condition ( & self , cond : & Expr < ' _ > ) -> bool {
167
+ self . let_chains_enabled || !matches ! ( cond. kind, ExprKind :: Let ( ..) )
168
+ }
163
169
}
164
170
165
171
impl_lint_pass ! ( CollapsibleIf => [ COLLAPSIBLE_IF , COLLAPSIBLE_ELSE_IF ] ) ;
@@ -174,11 +180,10 @@ impl LateLintPass<'_> for CollapsibleIf {
174
180
{
175
181
Self :: check_collapsible_else_if ( cx, then. span , else_) ;
176
182
} else if else_. is_none ( )
177
- && ! matches ! ( cond . kind , ExprKind :: Let ( .. ) )
183
+ && self . eligible_condition ( cond )
178
184
&& let ExprKind :: Block ( then, None ) = then. kind
179
185
{
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) ;
182
187
}
183
188
}
184
189
}
0 commit comments