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.
This commit is contained in:
bors 2019-02-10 12:47:16 +00:00
commit af43950143
3 changed files with 31 additions and 9 deletions

View File

@ -3,7 +3,7 @@
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::syntax::ast::LitKind;
use crate::utils::{is_direct_expn_of, span_help_and_lint};
use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
use if_chain::if_chain;
/// **What it does:** Check to call assert!(true/false)
@ -43,7 +43,9 @@ fn name(&self) -> &'static str {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
if_chain! {
if is_direct_expn_of(e.span, "assert").is_some();
if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
if !in_macro(assert_span)
|| is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| !in_macro(span));
if let ExprKind::Unary(_, ref lit) = e.node;
then {
if let ExprKind::Lit(ref inner) = lit.node {

View File

@ -1,3 +1,10 @@
macro_rules! assert_const {
($len:expr) => {
assert!($len > 0);
debug_assert!($len < 0);
};
}
fn main() {
assert!(true);
assert!(false);
@ -9,4 +16,8 @@ fn main() {
const C: bool = false;
assert!(C);
debug_assert!(true);
assert_const!(3);
assert_const!(-1);
}

View File

@ -1,5 +1,5 @@
error: assert!(true) will be optimized out by the compiler
--> $DIR/assertions_on_constants.rs:2:5
--> $DIR/assertions_on_constants.rs:9:5
|
LL | assert!(true);
| ^^^^^^^^^^^^^^
@ -8,7 +8,7 @@ LL | assert!(true);
= help: remove it
error: assert!(false) should probably be replaced
--> $DIR/assertions_on_constants.rs:3:5
--> $DIR/assertions_on_constants.rs:10:5
|
LL | assert!(false);
| ^^^^^^^^^^^^^^^
@ -16,7 +16,7 @@ LL | assert!(false);
= help: use panic!() or unreachable!()
error: assert!(true) will be optimized out by the compiler
--> $DIR/assertions_on_constants.rs:4:5
--> $DIR/assertions_on_constants.rs:11:5
|
LL | assert!(true, "true message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -24,7 +24,7 @@ LL | assert!(true, "true message");
= help: remove it
error: assert!(false) should probably be replaced
--> $DIR/assertions_on_constants.rs:5:5
--> $DIR/assertions_on_constants.rs:12:5
|
LL | assert!(false, "false message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -32,7 +32,7 @@ LL | assert!(false, "false message");
= help: use panic!() or unreachable!()
error: assert!(const: true) will be optimized out by the compiler
--> $DIR/assertions_on_constants.rs:8:5
--> $DIR/assertions_on_constants.rs:15:5
|
LL | assert!(B);
| ^^^^^^^^^^^
@ -40,12 +40,21 @@ LL | assert!(B);
= help: remove it
error: assert!(const: false) should probably be replaced
--> $DIR/assertions_on_constants.rs:11:5
--> $DIR/assertions_on_constants.rs:18:5
|
LL | assert!(C);
| ^^^^^^^^^^^
|
= help: use panic!() or unreachable!()
error: aborting due to 6 previous errors
error: assert!(true) will be optimized out by the compiler
--> $DIR/assertions_on_constants.rs:20:5
|
LL | debug_assert!(true);
| ^^^^^^^^^^^^^^^^^^^^
|
= help: remove it
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error: aborting due to 7 previous errors