1 use rustc::hir::{Expr, ExprKind};
2 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
3 use rustc::{declare_lint_pass, declare_tool_lint};
5 use crate::consts::{constant, Constant};
6 use crate::utils::{is_direct_expn_of, is_expn_of, span_help_and_lint};
9 /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
11 /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
12 /// panic!() or unreachable!()
14 /// **Known problems:** None
22 /// const B: bool = false;
25 pub ASSERTIONS_ON_CONSTANTS,
27 "`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
30 declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
32 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
33 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
34 let lint_assert_cb = |is_debug_assert: bool| {
35 if let ExprKind::Unary(_, ref lit) = e.kind {
36 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit) {
40 ASSERTIONS_ON_CONSTANTS,
42 "`assert!(true)` will be optimized out by the compiler",
45 } else if !is_debug_assert {
48 ASSERTIONS_ON_CONSTANTS,
50 "`assert!(false)` should probably be replaced",
51 "use `panic!()` or `unreachable!()`",
57 if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
58 if debug_assert_span.from_expansion() {
62 } else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
63 if assert_span.from_expansion() {
66 lint_assert_cb(false);