]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/assertions_on_constants.rs
Various cosmetic improvements.
[rust.git] / clippy_lints / src / assertions_on_constants.rs
1 use if_chain::if_chain;
2 use rustc::hir::{Expr, ExprKind};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
5
6 use crate::consts::{constant, Constant};
7 use crate::syntax::ast::LitKind;
8 use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
9
10 declare_clippy_lint! {
11     /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
12     ///
13     /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
14     /// panic!() or unreachable!()
15     ///
16     /// **Known problems:** None
17     ///
18     /// **Example:**
19     /// ```rust
20     /// assert!(false)
21     /// // or
22     /// assert!(true)
23     /// // or
24     /// const B: bool = false;
25     /// assert!(B)
26     /// ```
27     pub ASSERTIONS_ON_CONSTANTS,
28     style,
29     "`assert!(true)` / `assert!(false)` will be optimized out by the compiler, \
30      and should probably be replaced by a `panic!()` or `unreachable!()`"
31 }
32
33 pub struct AssertionsOnConstants;
34
35 impl LintPass for AssertionsOnConstants {
36     fn get_lints(&self) -> LintArray {
37         lint_array![ASSERTIONS_ON_CONSTANTS]
38     }
39
40     fn name(&self) -> &'static str {
41         "AssertionsOnConstants"
42     }
43 }
44
45 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
46     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
47         if_chain! {
48             if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
49             if !in_macro(assert_span)
50                 || is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| !in_macro(span));
51             if let ExprKind::Unary(_, ref lit) = e.node;
52             then {
53                 if let ExprKind::Lit(ref inner) = lit.node {
54                     match inner.node {
55                         LitKind::Bool(true) => {
56                             span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
57                                 "assert!(true) will be optimized out by the compiler",
58                                 "remove it");
59                         },
60                         LitKind::Bool(false) => {
61                             span_help_and_lint(
62                                 cx, ASSERTIONS_ON_CONSTANTS, e.span,
63                                 "assert!(false) should probably be replaced",
64                                 "use panic!() or unreachable!()");
65                         },
66                         _ => (),
67                     }
68                 } else if let Some(bool_const) = constant(cx, cx.tables, lit) {
69                     match bool_const.0 {
70                         Constant::Bool(true) => {
71                             span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
72                                 "assert!(const: true) will be optimized out by the compiler",
73                                 "remove it");
74                         },
75                         Constant::Bool(false) => {
76                             span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
77                                 "assert!(const: false) should probably be replaced",
78                                 "use panic!() or unreachable!()");
79                         },
80                         _ => (),
81                     }
82                 }
83             }
84         }
85     }
86 }