]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/assertions_on_constants.rs
Format code
[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_lint_pass, declare_tool_lint};
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 declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
33
34 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
35     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
36         let mut is_debug_assert = false;
37         if_chain! {
38             if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
39             if !in_macro(assert_span)
40                 || is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| {
41                     is_debug_assert = true;
42                     // Check that `debug_assert!` itself is not inside a macro
43                     !in_macro(span)
44                 });
45             if let ExprKind::Unary(_, ref lit) = e.node;
46             then {
47                 if let ExprKind::Lit(ref inner) = lit.node {
48                     match inner.node {
49                         LitKind::Bool(true) => {
50                             span_help_and_lint(
51                                 cx,
52                                 ASSERTIONS_ON_CONSTANTS,
53                                 e.span,
54                                 "`assert!(true)` will be optimized out by the compiler",
55                                 "remove it"
56                             );
57                         },
58                         LitKind::Bool(false) 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                         },
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(
73                                 cx,
74                                 ASSERTIONS_ON_CONSTANTS,
75                                 e.span,
76                                 "`assert!(const: true)` will be optimized out by the compiler",
77                                 "remove it"
78                             );
79                         },
80                         Constant::Bool(false) if !is_debug_assert => {
81                             span_help_and_lint(
82                                 cx,
83                                 ASSERTIONS_ON_CONSTANTS,
84                                 e.span,
85                                 "`assert!(const: false)` should probably be replaced",
86                                 "use `panic!()` or `unreachable!()`"
87                             );
88                         },
89                         _ => (),
90                     }
91                 }
92             }
93         }
94     }
95 }