|
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,42 +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 |
| - 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) => { |
| 34 | + let lint_assert_cb = |is_debug_assert: bool| { |
| 35 | + if let ExprKind::Unary(_, ref lit) = e.node { |
| 36 | + if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit) { |
| 37 | + if is_true { |
52 | 38 | span_help_and_lint(
|
53 | 39 | cx,
|
54 | 40 | ASSERTIONS_ON_CONSTANTS,
|
55 | 41 | e.span,
|
56 | 42 | "`assert!(true)` will be optimized out by the compiler",
|
57 |
| - "remove it" |
| 43 | + "remove it", |
58 | 44 | );
|
59 |
| - }, |
60 |
| - Constant::Bool(false) if !is_debug_assert => { |
| 45 | + } else if !is_debug_assert { |
61 | 46 | span_help_and_lint(
|
62 | 47 | cx,
|
63 | 48 | ASSERTIONS_ON_CONSTANTS,
|
64 | 49 | e.span,
|
65 | 50 | "`assert!(false)` should probably be replaced",
|
66 |
| - "use `panic!()` or `unreachable!()`" |
| 51 | + "use `panic!()` or `unreachable!()`", |
67 | 52 | );
|
68 |
| - }, |
69 |
| - _ => (), |
| 53 | + } |
70 | 54 | }
|
71 | 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); |
72 | 67 | }
|
73 | 68 | }
|
74 | 69 | }
|
0 commit comments