Skip to content

Commit 5c0488f

Browse files
committed
add assert to the parsing
1 parent 8c48a75 commit 5c0488f

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

clippy_utils/src/higher.rs

+38-31
Original file line numberDiff line numberDiff line change
@@ -452,41 +452,10 @@ impl<'tcx> AssertExpn<'tcx> {
452452
/// second_assert_argument: None, format_arg:None })` `debug_assert_eq!(a, b)` will return
453453
/// `Some(AssertExpn { first_assert_argument: a, second_assert_argument: Some(b),
454454
/// format_arg:None })`
455-
/// FIXME assert!
456455
pub fn parse(e: &'tcx Expr<'tcx>) -> Option<Self> {
457456
if let ExprKind::Block(block, _) = e.kind {
458457
if block.stmts.len() == 1 {
459458
if let StmtKind::Semi(matchexpr) = block.stmts.get(0)?.kind {
460-
// debug macros with unique arg: `debug_assert!` (e.g., `debug_assert!(some_condition)`)
461-
if_chain! {
462-
if let Some(If { cond, then, .. }) = If::hir(matchexpr);
463-
if let ExprKind::Unary(UnOp::Not, condition) = cond.kind;
464-
then {
465-
if_chain! {
466-
if let ExprKind::Block(block, _) = then.kind;
467-
if let Some(begin_panic_fmt_block) = block.expr;
468-
if let ExprKind::Block(block,_) = begin_panic_fmt_block.kind;
469-
if let Some(expr) = block.expr;
470-
if let ExprKind::Call(_, args_begin_panic_fmt) = expr.kind;
471-
if !args_begin_panic_fmt.is_empty();
472-
if let ExprKind::AddrOf(_, _, arg) = args_begin_panic_fmt[0].kind;
473-
if let Some(format_arg_expn) = FormatArgsExpn::parse(arg);
474-
then {
475-
return Some(Self {
476-
kind: AssertExpnKind::Bool(condition),
477-
format_arg: Some(format_arg_expn),
478-
is_debug: true,
479-
});
480-
}
481-
}
482-
return Some(Self {
483-
kind: AssertExpnKind::Bool(condition),
484-
format_arg: None,
485-
is_debug: true,
486-
});
487-
}
488-
}
489-
490459
// debug macros with two args: `debug_assert_{ne, eq}` (e.g., `assert_ne!(a, b)`)
491460
if_chain! {
492461
if let ExprKind::Block(matchblock,_) = matchexpr.kind;
@@ -495,11 +464,49 @@ impl<'tcx> AssertExpn<'tcx> {
495464
return Self::ast_matchblock(matchblock_expr, true);
496465
}
497466
}
467+
// debug macros with unique arg: `debug_assert!` (e.g., `debug_assert!(some_condition)`)
468+
return Self::ast_ifblock(matchexpr, true);
498469
}
499470
} else if let Some(matchblock_expr) = block.expr {
500471
// macros with two args: `assert_{ne, eq}` (e.g., `assert_ne!(a, b)`)
501472
return Self::ast_matchblock(matchblock_expr, false);
502473
}
474+
} else {
475+
// assert! macro
476+
return Self::ast_ifblock(e, false);
477+
}
478+
None
479+
}
480+
481+
/// Try to parse the pattern for an assert macro with a single argument like `{debug_}assert!`
482+
fn ast_ifblock(ifblock_expr: &'tcx Expr<'tcx>, is_debug: bool) -> Option<Self> {
483+
if_chain! {
484+
if let Some(If { cond, then, .. }) = If::hir(ifblock_expr);
485+
if let ExprKind::Unary(UnOp::Not, condition) = cond.kind;
486+
then {
487+
if_chain! {
488+
if let ExprKind::Block(block, _) = then.kind;
489+
if let Some(begin_panic_fmt_block) = block.expr;
490+
if let ExprKind::Block(block,_) = begin_panic_fmt_block.kind;
491+
if let Some(expr) = block.expr;
492+
if let ExprKind::Call(_, args_begin_panic_fmt) = expr.kind;
493+
if !args_begin_panic_fmt.is_empty();
494+
if let ExprKind::AddrOf(_, _, arg) = args_begin_panic_fmt[0].kind;
495+
if let Some(format_arg_expn) = FormatArgsExpn::parse(arg);
496+
then {
497+
return Some(Self {
498+
kind: AssertExpnKind::Bool(condition),
499+
format_arg: Some(format_arg_expn),
500+
is_debug
501+
});
502+
}
503+
}
504+
return Some(Self {
505+
kind: AssertExpnKind::Bool(condition),
506+
format_arg: None,
507+
is_debug
508+
});
509+
}
503510
}
504511
None
505512
}

0 commit comments

Comments
 (0)