Don't lint debug_assert!(false)

This commit is contained in:
flip1995
2019-04-18 11:47:39 +02:00
parent b834dbb2d5
commit be98df5ac3

View File

@@ -33,10 +33,15 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
let mut is_debug_assert = false;
if_chain! { if_chain! {
if let Some(assert_span) = is_direct_expn_of(e.span, "assert"); if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
if !in_macro(assert_span) 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; if let ExprKind::Unary(_, ref lit) = e.node;
then { then {
if let ExprKind::Lit(ref inner) = lit.node { 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", "assert!(true) will be optimized out by the compiler",
"remove it"); "remove it");
}, },
LitKind::Bool(false) => { LitKind::Bool(false) if !is_debug_assert => {
span_help_and_lint( span_help_and_lint(
cx, ASSERTIONS_ON_CONSTANTS, e.span, cx, ASSERTIONS_ON_CONSTANTS, e.span,
"assert!(false) should probably be replaced", "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", "assert!(const: true) will be optimized out by the compiler",
"remove it"); "remove it");
}, },
Constant::Bool(false) => { Constant::Bool(false) if !is_debug_assert => {
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span, span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
"assert!(const: false) should probably be replaced", "assert!(const: false) should probably be replaced",
"use panic!() or unreachable!()"); "use panic!() or unreachable!()");