]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/assertions_on_constants.rs
Remove code duplication
[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::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
8
9 declare_clippy_lint! {
10     /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
11     ///
12     /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
13     /// panic!() or unreachable!()
14     ///
15     /// **Known problems:** None
16     ///
17     /// **Example:**
18     /// ```rust,ignore
19     /// assert!(false)
20     /// // or
21     /// assert!(true)
22     /// // or
23     /// const B: bool = false;
24     /// assert!(B)
25     /// ```
26     pub ASSERTIONS_ON_CONSTANTS,
27     style,
28     "`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
29 }
30
31 declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
32
33 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
34     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
35         let mut is_debug_assert = false;
36         if_chain! {
37             if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
38             if !in_macro(assert_span)
39                 || is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| {
40                     is_debug_assert = true;
41                     // Check that `debug_assert!` itself is not inside a macro
42                     !in_macro(span)
43                 });
44             if let ExprKind::Unary(_, ref lit) = e.node;
45             if let Some(bool_const) = constant(cx, cx.tables, lit);
46             then {
47                 match bool_const.0 {
48                     Constant::Bool(true) => {
49                         span_help_and_lint(
50                             cx,
51                             ASSERTIONS_ON_CONSTANTS,
52                             e.span,
53                             "`assert!(true)` will be optimized out by the compiler",
54                             "remove it"
55                         );
56                     },
57                     Constant::Bool(false) if !is_debug_assert => {
58                         span_help_and_lint(
59                             cx,
60                             ASSERTIONS_ON_CONSTANTS,
61                             e.span,
62                             "`assert!(false)` should probably be replaced",
63                             "use `panic!()` or `unreachable!()`"
64                         );
65                     },
66                     _ => (),
67                 }
68             }
69         }
70     }
71 }