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