1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
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;
18 /// **What it does:** Check explicit call assert!(true/false)
20 /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a panic!() or unreachable!()
22 /// **Known problems:** None
30 /// const B: bool = false;
33 declare_clippy_lint! {
34 pub ASSERTIONS_ON_CONSTANTS,
36 "assert!(true/false) will be optimized out by the compiler/should probably be replaced by a panic!() or unreachable!()"
39 pub struct AssertionsOnConstants;
41 impl LintPass for AssertionsOnConstants {
42 fn get_lints(&self) -> LintArray {
43 lint_array![ASSERTIONS_ON_CONSTANTS]
47 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
48 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
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;
55 LitKind::Bool(true) => {
56 span_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
57 "assert!(true) will be optimized out by the compiler");
59 LitKind::Bool(false) => {
62 ASSERTIONS_ON_CONSTANTS,
64 "assert!(false) should probably be replaced",
66 "panic!()".to_string(),
67 Applicability::MachineApplicable);