Fix manual_map suggestion for if let.. else ... if let.. else chain

This commit is contained in:
Jason Newcomb
2021-03-05 15:57:37 -05:00
parent 6595d5571b
commit f468d82283
5 changed files with 67 additions and 6 deletions

View File

@@ -798,6 +798,26 @@ pub fn get_parent_as_impl(tcx: TyCtxt<'_>, id: HirId) -> Option<&Impl<'_>> {
}
}
/// Checks if the given expression is the else clause in the expression `if let .. {} else {}`
pub fn is_else_clause_of_if_let_else(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
let map = tcx.hir();
let mut iter = map.parent_iter(expr.hir_id);
let arm_id = match iter.next() {
Some((id, Node::Arm(..))) => id,
_ => return false,
};
match iter.next() {
Some((
_,
Node::Expr(Expr {
kind: ExprKind::Match(_, [_, else_arm], kind),
..
}),
)) => else_arm.hir_id == arm_id && matches!(kind, MatchSource::IfLetDesugar { .. }),
_ => false,
}
}
/// Checks whether the given expression is a constant integer of the given value.
/// unlike `is_integer_literal`, this version does const folding
pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool {