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};
6 use crate::consts::{constant, Constant};
7 use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
10 /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
12 /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
13 /// panic!() or unreachable!()
15 /// **Known problems:** None
23 /// const B: bool = false;
26 pub ASSERTIONS_ON_CONSTANTS,
28 "`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
31 declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
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;
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
44 if let ExprKind::Unary(_, ref lit) = e.node;
45 if let Some(bool_const) = constant(cx, cx.tables, lit);
48 Constant::Bool(true) => {
51 ASSERTIONS_ON_CONSTANTS,
53 "`assert!(true)` will be optimized out by the compiler",
57 Constant::Bool(false) if !is_debug_assert => {
60 ASSERTIONS_ON_CONSTANTS,
62 "`assert!(false)` should probably be replaced",
63 "use `panic!()` or `unreachable!()`"