Skip to content

Macro check for assertion_on_constants lint #3740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions clippy_lints/src/assertions_on_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/assertions_on_constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
macro_rules! assert_const {
($len:expr) => {
assert!($len > 0);
debug_assert!($len < 0);
};
}

fn main() {
assert!(true);
assert!(false);
Expand All @@ -9,4 +16,8 @@ fn main() {

const C: bool = false;
assert!(C);

debug_assert!(true);
assert_const!(3);
assert_const!(-1);
}
23 changes: 16 additions & 7 deletions tests/ui/assertions_on_constants.stderr
Original file line number Diff line number Diff line change
@@ -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);
| ^^^^^^^^^^^^^^
Expand All @@ -8,44 +8,53 @@ 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);
| ^^^^^^^^^^^^^^^
|
= 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");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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);
| ^^^^^^^^^^^
|
= 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