From be98df5ac3ef645a7d0743306dc11a4da61fc071 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 18 Apr 2019 11:47:39 +0200 Subject: [PATCH 1/6] Don't lint debug_assert!(false) --- clippy_lints/src/assertions_on_constants.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index ceea913233ee..d342e0cd02a6 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -33,10 +33,15 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { + let mut is_debug_assert = false; if_chain! { 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)); + || is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| { + is_debug_assert = true; + // Check that `debug_assert!` itself is not inside a macro + !in_macro(span) + }); if let ExprKind::Unary(_, ref lit) = e.node; then { if let ExprKind::Lit(ref inner) = lit.node { @@ -46,7 +51,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { "assert!(true) will be optimized out by the compiler", "remove it"); }, - LitKind::Bool(false) => { + LitKind::Bool(false) if !is_debug_assert => { span_help_and_lint( cx, ASSERTIONS_ON_CONSTANTS, e.span, "assert!(false) should probably be replaced", @@ -61,7 +66,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { "assert!(const: true) will be optimized out by the compiler", "remove it"); }, - Constant::Bool(false) => { + Constant::Bool(false) if !is_debug_assert => { span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span, "assert!(const: false) should probably be replaced", "use panic!() or unreachable!()"); From 5b836e344c4363595b820da4f4b924bcdabdf236 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 18 Apr 2019 11:48:19 +0200 Subject: [PATCH 2/6] Add test for debug_assert!(false) --- tests/ui/assertions_on_constants.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/ui/assertions_on_constants.rs b/tests/ui/assertions_on_constants.rs index 0d2953c7ed81..aee6cc505abd 100644 --- a/tests/ui/assertions_on_constants.rs +++ b/tests/ui/assertions_on_constants.rs @@ -18,6 +18,8 @@ fn main() { assert!(C); debug_assert!(true); + // Don't lint this, since there is no better way for expressing "Only panic in debug mode". + debug_assert!(false); // #3948 assert_const!(3); assert_const!(-1); } From 40218bae0c551ad18acf0ca02ced9086d615e9b3 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 18 Apr 2019 11:48:55 +0200 Subject: [PATCH 3/6] Format code --- clippy_lints/src/assertions_on_constants.rs | 39 ++++++++++++++------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index d342e0cd02a6..1750ffae837c 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -47,29 +47,44 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { if let ExprKind::Lit(ref inner) = lit.node { match inner.node { LitKind::Bool(true) => { - span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span, - "assert!(true) will be optimized out by the compiler", - "remove it"); + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(true)` will be optimized out by the compiler", + "remove it" + ); }, LitKind::Bool(false) if !is_debug_assert => { span_help_and_lint( - cx, ASSERTIONS_ON_CONSTANTS, e.span, - "assert!(false) should probably be replaced", - "use panic!() or unreachable!()"); + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(false)` should probably be replaced", + "use `panic!()` or `unreachable!()`" + ); }, _ => (), } } else if let Some(bool_const) = constant(cx, cx.tables, lit) { match bool_const.0 { Constant::Bool(true) => { - span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span, - "assert!(const: true) will be optimized out by the compiler", - "remove it"); + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(const: true)` will be optimized out by the compiler", + "remove it" + ); }, Constant::Bool(false) if !is_debug_assert => { - span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span, - "assert!(const: false) should probably be replaced", - "use panic!() or unreachable!()"); + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(const: false)` should probably be replaced", + "use `panic!()` or `unreachable!()`" + ); }, _ => (), } From 834ad7680642cbf8feac53b264b75a509bb48ed1 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 18 Apr 2019 12:04:46 +0200 Subject: [PATCH 4/6] Remove code duplication --- clippy_lints/src/assertions_on_constants.rs | 66 +++++++-------------- 1 file changed, 21 insertions(+), 45 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index 1750ffae837c..5b3310b28dfb 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -4,7 +4,6 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; use crate::consts::{constant, Constant}; -use crate::syntax::ast::LitKind; use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint}; declare_clippy_lint! { @@ -43,51 +42,28 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { !in_macro(span) }); if let ExprKind::Unary(_, ref lit) = e.node; + if let Some(bool_const) = constant(cx, cx.tables, lit); then { - if let ExprKind::Lit(ref inner) = lit.node { - match inner.node { - LitKind::Bool(true) => { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(true)` will be optimized out by the compiler", - "remove it" - ); - }, - LitKind::Bool(false) if !is_debug_assert => { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(false)` should probably be replaced", - "use `panic!()` or `unreachable!()`" - ); - }, - _ => (), - } - } else if let Some(bool_const) = constant(cx, cx.tables, lit) { - match bool_const.0 { - Constant::Bool(true) => { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(const: true)` will be optimized out by the compiler", - "remove it" - ); - }, - Constant::Bool(false) if !is_debug_assert => { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(const: false)` should probably be replaced", - "use `panic!()` or `unreachable!()`" - ); - }, - _ => (), - } + match bool_const.0 { + Constant::Bool(true) => { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(true)` will be optimized out by the compiler", + "remove it" + ); + }, + Constant::Bool(false) if !is_debug_assert => { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(false)` should probably be replaced", + "use `panic!()` or `unreachable!()`" + ); + }, + _ => (), } } } From 88359a136f517f8c828b8b2cdcf18421b0e5abe7 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 18 Apr 2019 12:05:09 +0200 Subject: [PATCH 5/6] Update *.stderr file --- tests/ui/assertions_on_constants.stderr | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/ui/assertions_on_constants.stderr b/tests/ui/assertions_on_constants.stderr index adfa326abacb..c22da856975c 100644 --- a/tests/ui/assertions_on_constants.stderr +++ b/tests/ui/assertions_on_constants.stderr @@ -1,4 +1,4 @@ -error: assert!(true) will be optimized out by the compiler +error: `assert!(true)` will be optimized out by the compiler --> $DIR/assertions_on_constants.rs:9:5 | LL | assert!(true); @@ -7,15 +7,15 @@ LL | assert!(true); = note: `-D clippy::assertions-on-constants` implied by `-D warnings` = help: remove it -error: assert!(false) should probably be replaced +error: `assert!(false)` should probably be replaced --> $DIR/assertions_on_constants.rs:10:5 | LL | assert!(false); | ^^^^^^^^^^^^^^^ | - = help: use panic!() or unreachable!() + = help: use `panic!()` or `unreachable!()` -error: assert!(true) will be optimized out by the compiler +error: `assert!(true)` will be optimized out by the compiler --> $DIR/assertions_on_constants.rs:11:5 | LL | assert!(true, "true message"); @@ -23,15 +23,15 @@ LL | assert!(true, "true message"); | = help: remove it -error: assert!(false) should probably be replaced +error: `assert!(false)` should probably be replaced --> $DIR/assertions_on_constants.rs:12:5 | LL | assert!(false, "false message"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: use panic!() or unreachable!() + = help: use `panic!()` or `unreachable!()` -error: assert!(const: true) will be optimized out by the compiler +error: `assert!(true)` will be optimized out by the compiler --> $DIR/assertions_on_constants.rs:15:5 | LL | assert!(B); @@ -39,15 +39,15 @@ LL | assert!(B); | = help: remove it -error: assert!(const: false) should probably be replaced +error: `assert!(false)` should probably be replaced --> $DIR/assertions_on_constants.rs:18:5 | LL | assert!(C); | ^^^^^^^^^^^ | - = help: use panic!() or unreachable!() + = help: use `panic!()` or `unreachable!()` -error: assert!(true) will be optimized out by the compiler +error: `assert!(true)` will be optimized out by the compiler --> $DIR/assertions_on_constants.rs:20:5 | LL | debug_assert!(true); From 10cd28900f79e013edbea5b4d9d956249e85439a Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 18 Apr 2019 13:37:20 +0200 Subject: [PATCH 6/6] Fix dogfood error --- clippy_lints/src/assertions_on_constants.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index 5b3310b28dfb..36ed835e2581 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -2,6 +2,7 @@ use if_chain::if_chain; use rustc::hir::{Expr, ExprKind}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; +use syntax_pos::Span; use crate::consts::{constant, Constant}; use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint}; @@ -33,14 +34,16 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { let mut is_debug_assert = false; + let debug_assert_not_in_macro = |span: Span| { + is_debug_assert = true; + // Check that `debug_assert!` itself is not inside a macro + !in_macro(span) + }; if_chain! { 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| { - is_debug_assert = true; - // Check that `debug_assert!` itself is not inside a macro - !in_macro(span) - }); + || is_direct_expn_of(assert_span, "debug_assert") + .map_or(false, debug_assert_not_in_macro); if let ExprKind::Unary(_, ref lit) = e.node; if let Some(bool_const) = constant(cx, cx.tables, lit); then {