From 446e2ecfb72cf91c8389a42357a168de77dd414b Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 5 Feb 2019 19:05:42 +0100 Subject: [PATCH 1/2] Don't warn about const assertions when assert is in a macro itself --- clippy_lints/src/assertions_on_constants.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index d420de3a4db..92ee9d1bc66 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -3,7 +3,7 @@ use crate::rustc::hir::{Expr, ExprKind}; 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 @@ impl LintPass for AssertionsOnConstants { 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 { From cb2d987ed4d4074e67ec307cbf5bfe289e876a3b Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 5 Feb 2019 19:06:08 +0100 Subject: [PATCH 2/2] Add tests for assertion_on_constants macro check --- tests/ui/assertions_on_constants.rs | 11 +++++++++++ tests/ui/assertions_on_constants.stderr | 23 ++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/tests/ui/assertions_on_constants.rs b/tests/ui/assertions_on_constants.rs index daeceebd3a2..0d2953c7ed8 100644 --- a/tests/ui/assertions_on_constants.rs +++ b/tests/ui/assertions_on_constants.rs @@ -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); } diff --git a/tests/ui/assertions_on_constants.stderr b/tests/ui/assertions_on_constants.stderr index e8001acceb1..adfa326abac 100644 --- a/tests/ui/assertions_on_constants.stderr +++ b/tests/ui/assertions_on_constants.stderr @@ -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