Skip to content

Commit be98df5

Browse files
committed
Don't lint debug_assert!(false)
1 parent b834dbb commit be98df5

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

clippy_lints/src/assertions_on_constants.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
3333

3434
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
3535
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
36+
let mut is_debug_assert = false;
3637
if_chain! {
3738
if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
3839
if !in_macro(assert_span)
39-
|| is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| !in_macro(span));
40+
|| is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| {
41+
is_debug_assert = true;
42+
// Check that `debug_assert!` itself is not inside a macro
43+
!in_macro(span)
44+
});
4045
if let ExprKind::Unary(_, ref lit) = e.node;
4146
then {
4247
if let ExprKind::Lit(ref inner) = lit.node {
@@ -46,7 +51,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
4651
"assert!(true) will be optimized out by the compiler",
4752
"remove it");
4853
},
49-
LitKind::Bool(false) => {
54+
LitKind::Bool(false) if !is_debug_assert => {
5055
span_help_and_lint(
5156
cx, ASSERTIONS_ON_CONSTANTS, e.span,
5257
"assert!(false) should probably be replaced",
@@ -61,7 +66,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
6166
"assert!(const: true) will be optimized out by the compiler",
6267
"remove it");
6368
},
64-
Constant::Bool(false) => {
69+
Constant::Bool(false) if !is_debug_assert => {
6570
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
6671
"assert!(const: false) should probably be replaced",
6772
"use panic!() or unreachable!()");

0 commit comments

Comments
 (0)