|
1 |
| -use if_chain::if_chain; |
2 | 1 | use rustc::hir::{Expr, ExprKind};
|
3 | 2 | use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
4 | 3 | use rustc::{declare_lint_pass, declare_tool_lint};
|
5 |
| -use syntax_pos::Span; |
6 | 4 |
|
7 | 5 | 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}; |
9 | 7 |
|
10 | 8 | declare_clippy_lint! {
|
11 | 9 | /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
|
@@ -33,40 +31,39 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
|
33 | 31 |
|
34 | 32 | impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
|
35 | 33 | 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| { |
47 | 35 | 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 | + ); |
67 | 53 | }
|
68 | 54 | }
|
69 | 55 | }
|
| 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); |
70 | 67 | }
|
71 | 68 | }
|
72 | 69 | }
|
0 commit comments