]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/assertions_on_constants.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[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,ignore
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, and should probably be replaced by a `panic!()` or `unreachable!()`"
30 }
31
32 pub struct AssertionsOnConstants;
33
34 impl LintPass for AssertionsOnConstants {
35     fn get_lints(&self) -> LintArray {
36         lint_array![ASSERTIONS_ON_CONSTANTS]
37     }
38
39     fn name(&self) -> &'static str {
40         "AssertionsOnConstants"
41     }
42 }
43
44 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
45     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
46         if_chain! {
47             if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
48             if !in_macro(assert_span)
49                 || is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| !in_macro(span));
50             if let ExprKind::Unary(_, ref lit) = e.node;
51             then {
52                 if let ExprKind::Lit(ref inner) = lit.node {
53                     match inner.node {
54                         LitKind::Bool(true) => {
55                             span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
56                                 "assert!(true) will be optimized out by the compiler",
57                                 "remove it");
58                         },
59                         LitKind::Bool(false) => {
60                             span_help_and_lint(
61                                 cx, ASSERTIONS_ON_CONSTANTS, e.span,
62                                 "assert!(false) should probably be replaced",
63                                 "use panic!() or unreachable!()");
64                         },
65                         _ => (),
66                     }
67                 } else if let Some(bool_const) = constant(cx, cx.tables, lit) {
68                     match bool_const.0 {
69                         Constant::Bool(true) => {
70                             span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
71                                 "assert!(const: true) will be optimized out by the compiler",
72                                 "remove it");
73                         },
74                         Constant::Bool(false) => {
75                             span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
76                                 "assert!(const: false) should probably be replaced",
77                                 "use panic!() or unreachable!()");
78                         },
79                         _ => (),
80                     }
81                 }
82             }
83         }
84     }
85 }