Skip to content

Commit 12e8075

Browse files
committed
Auto merge of #3989 - flip1995:assert_on_const, r=phansch
Don't trigger assertions_on_constants on debug_assert!(false) Fixes #3948 Fixes #3765 changelog: Fix `debug_assert!` false positive on `assertions_on_constants` lint
2 parents 0d9ef39 + 10cd289 commit 12e8075

File tree

3 files changed

+42
-41
lines changed

3 files changed

+42
-41
lines changed

clippy_lints/src/assertions_on_constants.rs

+30-31
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use if_chain::if_chain;
22
use rustc::hir::{Expr, ExprKind};
33
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
44
use rustc::{declare_lint_pass, declare_tool_lint};
5+
use syntax_pos::Span;
56

67
use crate::consts::{constant, Constant};
7-
use crate::syntax::ast::LitKind;
88
use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
99

1010
declare_clippy_lint! {
@@ -33,41 +33,40 @@ 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;
37+
let debug_assert_not_in_macro = |span: Span| {
38+
is_debug_assert = true;
39+
// Check that `debug_assert!` itself is not inside a macro
40+
!in_macro(span)
41+
};
3642
if_chain! {
3743
if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
3844
if !in_macro(assert_span)
39-
|| is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| !in_macro(span));
45+
|| is_direct_expn_of(assert_span, "debug_assert")
46+
.map_or(false, debug_assert_not_in_macro);
4047
if let ExprKind::Unary(_, ref lit) = e.node;
48+
if let Some(bool_const) = constant(cx, cx.tables, lit);
4149
then {
42-
if let ExprKind::Lit(ref inner) = lit.node {
43-
match inner.node {
44-
LitKind::Bool(true) => {
45-
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
46-
"assert!(true) will be optimized out by the compiler",
47-
"remove it");
48-
},
49-
LitKind::Bool(false) => {
50-
span_help_and_lint(
51-
cx, ASSERTIONS_ON_CONSTANTS, e.span,
52-
"assert!(false) should probably be replaced",
53-
"use panic!() or unreachable!()");
54-
},
55-
_ => (),
56-
}
57-
} else if let Some(bool_const) = constant(cx, cx.tables, lit) {
58-
match bool_const.0 {
59-
Constant::Bool(true) => {
60-
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
61-
"assert!(const: true) will be optimized out by the compiler",
62-
"remove it");
63-
},
64-
Constant::Bool(false) => {
65-
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
66-
"assert!(const: false) should probably be replaced",
67-
"use panic!() or unreachable!()");
68-
},
69-
_ => (),
70-
}
50+
match bool_const.0 {
51+
Constant::Bool(true) => {
52+
span_help_and_lint(
53+
cx,
54+
ASSERTIONS_ON_CONSTANTS,
55+
e.span,
56+
"`assert!(true)` will be optimized out by the compiler",
57+
"remove it"
58+
);
59+
},
60+
Constant::Bool(false) if !is_debug_assert => {
61+
span_help_and_lint(
62+
cx,
63+
ASSERTIONS_ON_CONSTANTS,
64+
e.span,
65+
"`assert!(false)` should probably be replaced",
66+
"use `panic!()` or `unreachable!()`"
67+
);
68+
},
69+
_ => (),
7170
}
7271
}
7372
}

tests/ui/assertions_on_constants.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ fn main() {
1818
assert!(C);
1919

2020
debug_assert!(true);
21+
// Don't lint this, since there is no better way for expressing "Only panic in debug mode".
22+
debug_assert!(false); // #3948
2123
assert_const!(3);
2224
assert_const!(-1);
2325
}

tests/ui/assertions_on_constants.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: assert!(true) will be optimized out by the compiler
1+
error: `assert!(true)` will be optimized out by the compiler
22
--> $DIR/assertions_on_constants.rs:9:5
33
|
44
LL | assert!(true);
@@ -7,47 +7,47 @@ LL | assert!(true);
77
= note: `-D clippy::assertions-on-constants` implied by `-D warnings`
88
= help: remove it
99

10-
error: assert!(false) should probably be replaced
10+
error: `assert!(false)` should probably be replaced
1111
--> $DIR/assertions_on_constants.rs:10:5
1212
|
1313
LL | assert!(false);
1414
| ^^^^^^^^^^^^^^^
1515
|
16-
= help: use panic!() or unreachable!()
16+
= help: use `panic!()` or `unreachable!()`
1717

18-
error: assert!(true) will be optimized out by the compiler
18+
error: `assert!(true)` will be optimized out by the compiler
1919
--> $DIR/assertions_on_constants.rs:11:5
2020
|
2121
LL | assert!(true, "true message");
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323
|
2424
= help: remove it
2525

26-
error: assert!(false) should probably be replaced
26+
error: `assert!(false)` should probably be replaced
2727
--> $DIR/assertions_on_constants.rs:12:5
2828
|
2929
LL | assert!(false, "false message");
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3131
|
32-
= help: use panic!() or unreachable!()
32+
= help: use `panic!()` or `unreachable!()`
3333

34-
error: assert!(const: true) will be optimized out by the compiler
34+
error: `assert!(true)` will be optimized out by the compiler
3535
--> $DIR/assertions_on_constants.rs:15:5
3636
|
3737
LL | assert!(B);
3838
| ^^^^^^^^^^^
3939
|
4040
= help: remove it
4141

42-
error: assert!(const: false) should probably be replaced
42+
error: `assert!(false)` should probably be replaced
4343
--> $DIR/assertions_on_constants.rs:18:5
4444
|
4545
LL | assert!(C);
4646
| ^^^^^^^^^^^
4747
|
48-
= help: use panic!() or unreachable!()
48+
= help: use `panic!()` or `unreachable!()`
4949

50-
error: assert!(true) will be optimized out by the compiler
50+
error: `assert!(true)` will be optimized out by the compiler
5151
--> $DIR/assertions_on_constants.rs:20:5
5252
|
5353
LL | debug_assert!(true);

0 commit comments

Comments
 (0)