Skip to content

Commit e7af60f

Browse files
committed
Add util function for desugaring if block
1 parent c74aac9 commit e7af60f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

clippy_lints/src/utils/higher.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@ pub fn for_loop(expr: &hir::Expr) -> Option<(&hir::Pat, &hir::Expr, &hir::Expr)>
199199
None
200200
}
201201

202+
/// Recover the essential nodes of a desugared if block
203+
/// `if cond { then } else { els }` becomes `(cond, then, Some(els))`
204+
pub fn if_block(expr: &hir::Expr) -> Option<(&hir::Expr, &hir::Expr, Option<&hir::Expr>)> {
205+
if let hir::ExprKind::Match(ref cond, ref arms, hir::MatchSource::IfDesugar { contains_else_clause }) = expr.node {
206+
let cond = if let hir::ExprKind::DropTemps(ref cond) = cond.node {
207+
cond
208+
} else {
209+
panic!("If block desugar must contain DropTemps");
210+
};
211+
let then = &arms[0].body;
212+
let els = if contains_else_clause {
213+
Some(&*arms[1].body)
214+
} else {
215+
None
216+
};
217+
Some((cond, then, els))
218+
} else {
219+
None
220+
}
221+
}
222+
202223
/// Represent the pre-expansion arguments of a `vec!` invocation.
203224
pub enum VecArgs<'a> {
204225
/// `vec![elem; len]`

0 commit comments

Comments
 (0)