1 use clippy_utils::consts::{constant, Constant};
2 use clippy_utils::diagnostics::span_lint_and_help;
3 use clippy_utils::source::snippet_opt;
4 use clippy_utils::{is_direct_expn_of, is_expn_of, match_panic_call};
5 use if_chain::if_chain;
6 use rustc_hir::{Expr, ExprKind, UnOp};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
12 /// Checks for `assert!(true)` and `assert!(false)` calls.
14 /// ### Why is this bad?
15 /// Will be optimized out by the compiler or should probably be replaced by a
16 /// `panic!()` or `unreachable!()`
18 /// ### Known problems
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<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
36 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
37 let lint_true = |is_debug: bool| {
40 ASSERTIONS_ON_CONSTANTS,
43 "`debug_assert!(true)` will be optimized out by the compiler"
45 "`assert!(true)` will be optimized out by the compiler"
51 let lint_false_without_message = || {
54 ASSERTIONS_ON_CONSTANTS,
56 "`assert!(false)` should probably be replaced",
58 "use `panic!()` or `unreachable!()`",
61 let lint_false_with_message = |panic_message: String| {
64 ASSERTIONS_ON_CONSTANTS,
66 &format!("`assert!(false, {})` should probably be replaced", panic_message),
68 &format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message),
72 if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
73 if debug_assert_span.from_expansion() {
77 if let ExprKind::Unary(_, lit) = e.kind;
78 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), lit);
84 } else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
85 if assert_span.from_expansion() {
88 if let Some(assert_match) = match_assert_with_message(cx, e) {
90 // matched assert but not message
91 AssertKind::WithoutMessage(false) => lint_false_without_message(),
92 AssertKind::WithoutMessage(true) | AssertKind::WithMessage(_, true) => lint_true(false),
93 AssertKind::WithMessage(panic_message, false) => lint_false_with_message(panic_message),
100 /// Result of calling `match_assert_with_message`.
102 WithMessage(String, bool),
103 WithoutMessage(bool),
106 /// Check if the expression matches
111 /// ::std::rt::begin_panic(message, _)
116 /// where `message` is any expression and `c` is a constant bool.
117 fn match_assert_with_message<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<AssertKind> {
119 if let ExprKind::If(cond, then, _) = expr.kind;
120 if let ExprKind::Unary(UnOp::Not, expr) = cond.kind;
121 // bind the first argument of the `assert!` macro
122 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), expr);
124 if let ExprKind::Block(block, _) = then.kind;
125 if block.stmts.is_empty();
126 if let Some(block_expr) = &block.expr;
127 // inner block is optional. unwrap it if it exists, or use the expression as is otherwise.
128 if let Some(begin_panic_call) = match block_expr.kind {
129 ExprKind::Block(inner_block, _) => &inner_block.expr,
133 if let Some(arg) = match_panic_call(cx, begin_panic_call);
134 // bind the second argument of the `assert!` macro if it exists
135 if let panic_message = snippet_opt(cx, arg.span);
136 // second argument of begin_panic is irrelevant
137 // as is the second match arm
139 // an empty message occurs when it was generated by the macro
140 // (and not passed by the user)
142 .filter(|msg| !msg.is_empty())
143 .map(|msg| AssertKind::WithMessage(msg, is_true))
144 .or(Some(AssertKind::WithoutMessage(is_true)));