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};
7 use crate::consts::{constant, Constant};
8 use crate::utils::{in_macro_or_desugar, is_direct_expn_of, span_help_and_lint};
11 declare_clippy_lint! {
12 /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
14 /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
15 /// panic!() or unreachable!()
17 /// **Known problems:** None
25 /// const B: bool = false;
28 pub ASSERTIONS_ON_CONSTANTS,
30 "`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
33 declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
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)
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);
52 Constant::Bool(true) => {
55 ASSERTIONS_ON_CONSTANTS,
57 "`assert!(true)` will be optimized out by the compiler",
61 Constant::Bool(false) if !is_debug_assert => {
64 ASSERTIONS_ON_CONSTANTS,
66 "`assert!(false)` should probably be replaced",
67 "use `panic!()` or `unreachable!()`"