@@ -54,7 +54,7 @@ impl<'tcx> ForLoop<'tcx> {
54
54
}
55
55
}
56
56
57
- /// An `if` expression without `DropTemps `
57
+ /// An `if` expression without `let `
58
58
pub struct If < ' hir > {
59
59
/// `if` condition
60
60
pub cond : & ' hir Expr < ' hir > ,
@@ -66,16 +66,10 @@ pub struct If<'hir> {
66
66
67
67
impl < ' hir > If < ' hir > {
68
68
#[ inline]
69
- /// Parses an `if` expression
69
+ /// Parses an `if` expression without `let`
70
70
pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
71
- if let ExprKind :: If (
72
- Expr {
73
- kind : ExprKind :: DropTemps ( cond) ,
74
- ..
75
- } ,
76
- then,
77
- r#else,
78
- ) = expr. kind
71
+ if let ExprKind :: If ( cond, then, r#else) = expr. kind
72
+ && !has_let_expr ( cond)
79
73
{
80
74
Some ( Self { cond, then, r#else } )
81
75
} else {
@@ -198,18 +192,10 @@ impl<'hir> IfOrIfLet<'hir> {
198
192
/// Parses an `if` or `if let` expression
199
193
pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
200
194
if let ExprKind :: If ( cond, then, r#else) = expr. kind {
201
- if let ExprKind :: DropTemps ( new_cond) = cond. kind {
202
- return Some ( Self {
203
- cond : new_cond,
204
- then,
205
- r#else,
206
- } ) ;
207
- }
208
- if let ExprKind :: Let ( ..) = cond. kind {
209
- return Some ( Self { cond, then, r#else } ) ;
210
- }
195
+ Some ( Self { cond, then, r#else } )
196
+ } else {
197
+ None
211
198
}
212
- None
213
199
}
214
200
}
215
201
@@ -343,15 +329,7 @@ impl<'hir> While<'hir> {
343
329
Block {
344
330
expr :
345
331
Some ( Expr {
346
- kind :
347
- ExprKind :: If (
348
- Expr {
349
- kind : ExprKind :: DropTemps ( condition) ,
350
- ..
351
- } ,
352
- body,
353
- _,
354
- ) ,
332
+ kind : ExprKind :: If ( condition, body, _) ,
355
333
..
356
334
} ) ,
357
335
..
@@ -360,6 +338,7 @@ impl<'hir> While<'hir> {
360
338
LoopSource :: While ,
361
339
span,
362
340
) = expr. kind
341
+ && !has_let_expr ( condition)
363
342
{
364
343
return Some ( Self { condition, body, span } ) ;
365
344
}
@@ -493,3 +472,13 @@ pub fn get_vec_init_kind<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -
493
472
}
494
473
None
495
474
}
475
+
476
+ /// Checks that a condition doesn't have a `let` expression, to keep `If` and `While` from accepting
477
+ /// `if let` and `while let`.
478
+ pub const fn has_let_expr < ' tcx > ( cond : & ' tcx Expr < ' tcx > ) -> bool {
479
+ match & cond. kind {
480
+ ExprKind :: Let ( _) => true ,
481
+ ExprKind :: Binary ( _, lhs, rhs) => has_let_expr ( lhs) || has_let_expr ( rhs) ,
482
+ _ => false ,
483
+ }
484
+ }
0 commit comments