Skip to content

Commit 021748c

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

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

clippy_lints/src/utils/higher.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ 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 {..}) = expr.node {
206+
let then = &arms[0].body;
207+
let els = arms.get(1).map(|x| &*x.body);
208+
Some((cond, then, els))
209+
} else {
210+
None
211+
}
212+
}
213+
202214
/// Represent the pre-expansion arguments of a `vec!` invocation.
203215
pub enum VecArgs<'a> {
204216
/// `vec![elem; len]`

0 commit comments

Comments
 (0)