|
7 | 7 | // option. This file may not be copied, modified, or distributed
|
8 | 8 | // except according to those terms.
|
9 | 9 |
|
| 10 | +use crate::consts::{constant, Constant}; |
10 | 11 | use crate::rustc::hir::{Expr, ExprKind};
|
11 | 12 | use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
12 | 13 | use crate::rustc::{declare_tool_lint, lint_array};
|
13 | 14 | use crate::syntax::ast::LitKind;
|
14 |
| -use crate::utils::{is_direct_expn_of, span_lint, span_lint_and_sugg}; |
15 |
| -use rustc_errors::Applicability; |
| 15 | +use crate::utils::{is_direct_expn_of, span_help_and_lint}; |
16 | 16 | use if_chain::if_chain;
|
17 | 17 |
|
18 |
| -/// **What it does:** Check explicit call assert!(true/false) |
| 18 | +/// **What it does:** Check to call assert!(true/false) |
19 | 19 | ///
|
20 |
| -/// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a panic!() or unreachable!() |
| 20 | +/// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a |
| 21 | +/// panic!() or unreachable!() |
21 | 22 | ///
|
22 | 23 | /// **Known problems:** None
|
23 | 24 | ///
|
@@ -49,24 +50,36 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
|
49 | 50 | if_chain! {
|
50 | 51 | if is_direct_expn_of(e.span, "assert").is_some();
|
51 | 52 | if let ExprKind::Unary(_, ref lit) = e.node;
|
52 |
| - if let ExprKind::Lit(ref inner) = lit.node; |
53 | 53 | then {
|
54 |
| - match inner.node { |
55 |
| - LitKind::Bool(true) => { |
56 |
| - span_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span, |
57 |
| - "assert!(true) will be optimized out by the compiler"); |
58 |
| - }, |
59 |
| - LitKind::Bool(false) => { |
60 |
| - span_lint_and_sugg( |
61 |
| - cx, |
62 |
| - ASSERTIONS_ON_CONSTANTS, |
63 |
| - e.span, |
64 |
| - "assert!(false) should probably be replaced", |
65 |
| - "try", |
66 |
| - "panic!()".to_string(), |
67 |
| - Applicability::MachineApplicable); |
68 |
| - }, |
69 |
| - _ => (), |
| 54 | + if let ExprKind::Lit(ref inner) = lit.node { |
| 55 | + match inner.node { |
| 56 | + LitKind::Bool(true) => { |
| 57 | + span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span, |
| 58 | + "assert!(true) will be optimized out by the compiler", |
| 59 | + "remove it"); |
| 60 | + }, |
| 61 | + LitKind::Bool(false) => { |
| 62 | + span_help_and_lint( |
| 63 | + cx, ASSERTIONS_ON_CONSTANTS, e.span, |
| 64 | + "assert!(false) should probably be replaced", |
| 65 | + "use panic!() or unreachable!()"); |
| 66 | + }, |
| 67 | + _ => (), |
| 68 | + } |
| 69 | + } else if let Some(bool_const) = constant(cx, cx.tables, lit) { |
| 70 | + match bool_const.0 { |
| 71 | + Constant::Bool(true) => { |
| 72 | + span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span, |
| 73 | + "assert!(const: true) will be optimized out by the compiler", |
| 74 | + "remove it"); |
| 75 | + }, |
| 76 | + Constant::Bool(false) => { |
| 77 | + span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span, |
| 78 | + "assert!(const: false) should probably be replaced", |
| 79 | + "use panic!() or unreachable!()"); |
| 80 | + }, |
| 81 | + _ => (), |
| 82 | + } |
70 | 83 | }
|
71 | 84 | }
|
72 | 85 | }
|
|
0 commit comments