]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/assertions_on_constants.rs
Auto merge of #3987 - phansch:rustfix_option_map_or_none, 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_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         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| !in_macro(span));
40             if let ExprKind::Unary(_, ref lit) = e.node;
41             then {
42                 if let ExprKind::Lit(ref inner) = lit.node {
43                     match inner.node {
44                         LitKind::Bool(true) => {
45                             span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
46                                 "assert!(true) will be optimized out by the compiler",
47                                 "remove it");
48                         },
49                         LitKind::Bool(false) => {
50                             span_help_and_lint(
51                                 cx, ASSERTIONS_ON_CONSTANTS, e.span,
52                                 "assert!(false) should probably be replaced",
53                                 "use panic!() or unreachable!()");
54                         },
55                         _ => (),
56                     }
57                 } else if let Some(bool_const) = constant(cx, cx.tables, lit) {
58                     match bool_const.0 {
59                         Constant::Bool(true) => {
60                             span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
61                                 "assert!(const: true) will be optimized out by the compiler",
62                                 "remove it");
63                         },
64                         Constant::Bool(false) => {
65                             span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
66                                 "assert!(const: false) should probably be replaced",
67                                 "use panic!() or unreachable!()");
68                         },
69                         _ => (),
70                     }
71                 }
72             }
73         }
74     }
75 }