Skip to content

Commit af43950

Browse files
committed
Auto merge of #3740 - flip1995:const_assert_macro, r=oli-obk
Macro check for assertion_on_constants lint The `assertion_on_constants` lint currently has following output for this code [Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6f2c9df6fc50baf847212d3b5136ee97): ```rust macro_rules! assert_const { ($len:expr) => { assert!($len > 0); } } fn main() { assert_const!(3); assert_const!(-1); } ``` ``` warning: assert!(const: true) will be optimized out by the compiler --> src/main.rs:3:9 | 3 | assert!($len > 0); | ^^^^^^^^^^^^^^^^^^ ... 8 | assert_const!(3); | ---------------- in this macro invocation | = note: #[warn(clippy::assertions_on_constants)] on by default = help: remove it = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants warning: assert!(const: false) should probably be replaced --> src/main.rs:3:9 | 3 | assert!($len > 0); | ^^^^^^^^^^^^^^^^^^ ... 9 | assert_const!(-1); | ----------------- in this macro invocation | = help: use panic!() or unreachable!() = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants ``` This is contradictory. This lint should not trigger if the `assert!` is in a macro itself.
2 parents 83edb64 + cb2d987 commit af43950

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

clippy_lints/src/assertions_on_constants.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::rustc::hir::{Expr, ExprKind};
33
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
44
use crate::rustc::{declare_tool_lint, lint_array};
55
use crate::syntax::ast::LitKind;
6-
use crate::utils::{is_direct_expn_of, span_help_and_lint};
6+
use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
77
use if_chain::if_chain;
88

99
/// **What it does:** Check to call assert!(true/false)
@@ -43,7 +43,9 @@ impl LintPass for AssertionsOnConstants {
4343
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
4444
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
4545
if_chain! {
46-
if is_direct_expn_of(e.span, "assert").is_some();
46+
if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
47+
if !in_macro(assert_span)
48+
|| is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| !in_macro(span));
4749
if let ExprKind::Unary(_, ref lit) = e.node;
4850
then {
4951
if let ExprKind::Lit(ref inner) = lit.node {

tests/ui/assertions_on_constants.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
macro_rules! assert_const {
2+
($len:expr) => {
3+
assert!($len > 0);
4+
debug_assert!($len < 0);
5+
};
6+
}
7+
18
fn main() {
29
assert!(true);
310
assert!(false);
@@ -9,4 +16,8 @@ fn main() {
916

1017
const C: bool = false;
1118
assert!(C);
19+
20+
debug_assert!(true);
21+
assert_const!(3);
22+
assert_const!(-1);
1223
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: assert!(true) will be optimized out by the compiler
2-
--> $DIR/assertions_on_constants.rs:2:5
2+
--> $DIR/assertions_on_constants.rs:9:5
33
|
44
LL | assert!(true);
55
| ^^^^^^^^^^^^^^
@@ -8,44 +8,53 @@ LL | assert!(true);
88
= help: remove it
99

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

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

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

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

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

50-
error: aborting due to 6 previous errors
50+
error: assert!(true) will be optimized out by the compiler
51+
--> $DIR/assertions_on_constants.rs:20:5
52+
|
53+
LL | debug_assert!(true);
54+
| ^^^^^^^^^^^^^^^^^^^^
55+
|
56+
= help: remove it
57+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
58+
59+
error: aborting due to 7 previous errors
5160

0 commit comments

Comments
 (0)