Skip to content

Commit 533bdfb

Browse files
committed
Fix assertions_on_constants lint
1 parent 439284b commit 533bdfb

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed
Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use if_chain::if_chain;
21
use rustc::hir::{Expr, ExprKind};
32
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
43
use rustc::{declare_lint_pass, declare_tool_lint};
5-
use syntax_pos::Span;
64

75
use crate::consts::{constant, Constant};
8-
use crate::utils::{in_macro_or_desugar, is_direct_expn_of, span_help_and_lint};
6+
use crate::utils::{in_macro_or_desugar, is_direct_expn_of, is_expn_of, span_help_and_lint};
97

108
declare_clippy_lint! {
119
/// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
@@ -33,40 +31,39 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
3331

3432
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
3533
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
36-
let mut is_debug_assert = false;
37-
if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
38-
if in_macro_or_desugar(assert_span) {
39-
return;
40-
}
41-
if let Some(debug_assert_span) = is_direct_expn_of(assert_span, "debug_assert") {
42-
if in_macro_or_desugar(debug_assert_span) {
43-
return;
44-
}
45-
is_debug_assert = true;
46-
}
34+
let lint_assert_cb = |is_debug_assert: bool| {
4735
if let ExprKind::Unary(_, ref lit) = e.node {
48-
if let Some((bool_const, _)) = constant(cx, cx.tables, lit) {
49-
if let Constant::Bool(is_true) bool_const {
50-
if is_true {
51-
span_help_and_lint(
52-
cx,
53-
ASSERTIONS_ON_CONSTANTS,
54-
e.span,
55-
"`assert!(true)` will be optimized out by the compiler",
56-
"remove it"
57-
);
58-
} else if !is_debug_assert {
59-
span_help_and_lint(
60-
cx,
61-
ASSERTIONS_ON_CONSTANTS,
62-
e.span,
63-
"`assert!(false)` should probably be replaced",
64-
"use `panic!()` or `unreachable!()`"
65-
);
66-
}
36+
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit) {
37+
if is_true {
38+
span_help_and_lint(
39+
cx,
40+
ASSERTIONS_ON_CONSTANTS,
41+
e.span,
42+
"`assert!(true)` will be optimized out by the compiler",
43+
"remove it",
44+
);
45+
} else if !is_debug_assert {
46+
span_help_and_lint(
47+
cx,
48+
ASSERTIONS_ON_CONSTANTS,
49+
e.span,
50+
"`assert!(false)` should probably be replaced",
51+
"use `panic!()` or `unreachable!()`",
52+
);
6753
}
6854
}
6955
}
56+
};
57+
if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
58+
if in_macro_or_desugar(debug_assert_span) {
59+
return;
60+
}
61+
lint_assert_cb(true);
62+
} else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
63+
if in_macro_or_desugar(assert_span) {
64+
return;
65+
}
66+
lint_assert_cb(false);
7067
}
7168
}
7269
}

0 commit comments

Comments
 (0)