Skip to content

Commit 439284b

Browse files
committed
Make code cleaner and more readable
1 parent 49dff2c commit 439284b

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

clippy_lints/src/assertions_on_constants.rs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,37 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
3434
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
3535
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
3636
let mut is_debug_assert = false;
37-
let debug_assert_not_in_macro_or_desugar = |span: Span| {
38-
is_debug_assert = true;
39-
// Check that `debug_assert!` itself is not inside a macro
40-
!in_macro_or_desugar(span)
41-
};
42-
if_chain! {
43-
if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
44-
if !in_macro_or_desugar(assert_span)
45-
|| is_direct_expn_of(assert_span, "debug_assert")
46-
.map_or(false, debug_assert_not_in_macro_or_desugar);
47-
if let ExprKind::Unary(_, ref lit) = e.node;
48-
if let Some(bool_const) = constant(cx, cx.tables, lit);
49-
then {
50-
match bool_const.0 {
51-
Constant::Bool(true) => {
52-
span_help_and_lint(
53-
cx,
54-
ASSERTIONS_ON_CONSTANTS,
55-
e.span,
56-
"`assert!(true)` will be optimized out by the compiler",
57-
"remove it"
58-
);
59-
},
60-
Constant::Bool(false) if !is_debug_assert => {
61-
span_help_and_lint(
62-
cx,
63-
ASSERTIONS_ON_CONSTANTS,
64-
e.span,
65-
"`assert!(false)` should probably be replaced",
66-
"use `panic!()` or `unreachable!()`"
67-
);
68-
},
69-
_ => (),
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+
}
47+
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+
}
67+
}
7068
}
7169
}
7270
}

0 commit comments

Comments
 (0)