@@ -54,7 +54,7 @@ impl<'tcx> ForLoop<'tcx> {
5454 }
5555}
5656
57- /// An `if` expression without `DropTemps `
57+ /// An `if` expression without `let `
5858pub struct If < ' hir > {
5959 /// `if` condition
6060 pub cond : & ' hir Expr < ' hir > ,
@@ -66,16 +66,10 @@ pub struct If<'hir> {
6666
6767impl < ' hir > If < ' hir > {
6868 #[ inline]
69- /// Parses an `if` expression
69+ /// Parses an `if` expression without `let`
7070 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)
7973 {
8074 Some ( Self { cond, then, r#else } )
8175 } else {
@@ -198,18 +192,10 @@ impl<'hir> IfOrIfLet<'hir> {
198192 /// Parses an `if` or `if let` expression
199193 pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
200194 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
211198 }
212- None
213199 }
214200}
215201
@@ -343,15 +329,7 @@ impl<'hir> While<'hir> {
343329 Block {
344330 expr :
345331 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, _) ,
355333 ..
356334 } ) ,
357335 ..
@@ -360,6 +338,7 @@ impl<'hir> While<'hir> {
360338 LoopSource :: While ,
361339 span,
362340 ) = expr. kind
341+ && !has_let_expr ( condition)
363342 {
364343 return Some ( Self { condition, body, span } ) ;
365344 }
@@ -493,3 +472,13 @@ pub fn get_vec_init_kind<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -
493472 }
494473 None
495474}
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