]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/assertions_on_constants.rs
change assert_checks to assertions_on_constants
[rust.git] / clippy_lints / src / assertions_on_constants.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::rustc::hir::{Expr, ExprKind};
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::syntax::ast::LitKind;
14 use crate::utils::{is_direct_expn_of, span_lint, span_lint_and_sugg};
15 use rustc_errors::Applicability;
16 use if_chain::if_chain;
17
18 /// **What it does:** Check explicit call assert!(true/false)
19 ///
20 /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a panic!() or unreachable!()
21 ///
22 /// **Known problems:** None
23 ///
24 /// **Example:**
25 /// ```rust
26 /// assert!(false)
27 /// // or
28 /// assert!(true)
29 /// // or
30 /// const B: bool = false;
31 /// assert!(B)
32 /// ```
33 declare_clippy_lint! {
34     pub ASSERTIONS_ON_CONSTANTS,
35     style,
36     "assert!(true/false) will be optimized out by the compiler/should probably be replaced by a panic!() or unreachable!()"
37 }
38
39 pub struct AssertionsOnConstants;
40
41 impl LintPass for AssertionsOnConstants {
42     fn get_lints(&self) -> LintArray {
43         lint_array![ASSERTIONS_ON_CONSTANTS]
44     }
45 }
46
47 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
48     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
49         if_chain! {
50             if is_direct_expn_of(e.span, "assert").is_some();
51             if let ExprKind::Unary(_, ref lit) = e.node;
52             if let ExprKind::Lit(ref inner) = lit.node;
53             then {
54                 match inner.node {
55                     LitKind::Bool(true) => {
56                         span_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
57                             "assert!(true) will be optimized out by the compiler");
58                     },
59                     LitKind::Bool(false) => {
60                         span_lint_and_sugg(
61                             cx,
62                             ASSERTIONS_ON_CONSTANTS,
63                             e.span,
64                             "assert!(false) should probably be replaced",
65                             "try",
66                             "panic!()".to_string(),
67                             Applicability::MachineApplicable);
68                     },
69                     _ => (),
70                 }
71             }
72         }
73     }
74 }