1 use crate::consts::{constant, Constant};
2 use crate::utils::{is_direct_expn_of, is_expn_of, match_panic_call, snippet_opt, span_lint_and_help};
3 use if_chain::if_chain;
4 use rustc_ast::ast::LitKind;
5 use rustc_hir::{Expr, ExprKind, PatKind, UnOp};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_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
21 /// const B: bool = false;
24 pub ASSERTIONS_ON_CONSTANTS,
26 "`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
29 declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
31 impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
32 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
33 let lint_true = |is_debug: bool| {
36 ASSERTIONS_ON_CONSTANTS,
39 "`debug_assert!(true)` will be optimized out by the compiler"
41 "`assert!(true)` will be optimized out by the compiler"
47 let lint_false_without_message = || {
50 ASSERTIONS_ON_CONSTANTS,
52 "`assert!(false)` should probably be replaced",
54 "use `panic!()` or `unreachable!()`",
57 let lint_false_with_message = |panic_message: String| {
60 ASSERTIONS_ON_CONSTANTS,
62 &format!("`assert!(false, {})` should probably be replaced", panic_message),
64 &format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message),
68 if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
69 if debug_assert_span.from_expansion() {
73 if let ExprKind::Unary(_, ref lit) = e.kind;
74 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), lit);
80 } else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
81 if assert_span.from_expansion() {
84 if let Some(assert_match) = match_assert_with_message(&cx, e) {
86 // matched assert but not message
87 AssertKind::WithoutMessage(false) => lint_false_without_message(),
88 AssertKind::WithoutMessage(true) | AssertKind::WithMessage(_, true) => lint_true(false),
89 AssertKind::WithMessage(panic_message, false) => lint_false_with_message(panic_message),
96 /// Result of calling `match_assert_with_message`.
98 WithMessage(String, bool),
102 /// Check if the expression matches
105 /// match { let _t = !c; _t } {
108 /// ::std::rt::begin_panic(message, _)
115 /// where `message` is any expression and `c` is a constant bool.
116 fn match_assert_with_message<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<AssertKind> {
118 if let ExprKind::Match(ref expr, ref arms, _) = expr.kind;
119 // matches { let _t = expr; _t }
120 if let ExprKind::DropTemps(ref expr) = expr.kind;
121 if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind;
122 // bind the first argument of the `assert!` macro
123 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), expr);
125 if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind;
126 if let ExprKind::Lit(ref lit) = lit_expr.kind;
127 if let LitKind::Bool(true) = lit.node;
129 if let ExprKind::Block(ref block, _) = arms[0].body.kind;
130 if block.stmts.is_empty();
131 if let Some(block_expr) = &block.expr;
132 // inner block is optional. unwrap it if it exists, or use the expression as is otherwise.
133 if let Some(begin_panic_call) = match block_expr.kind {
134 ExprKind::Block(ref inner_block, _) => &inner_block.expr,
138 if let Some(args) = match_panic_call(cx, begin_panic_call);
140 // bind the second argument of the `assert!` macro if it exists
141 if let panic_message = snippet_opt(cx, args[0].span);
142 // second argument of begin_panic is irrelevant
143 // as is the second match arm
145 // an empty message occurs when it was generated by the macro
146 // (and not passed by the user)
148 .filter(|msg| !msg.is_empty())
149 .map(|msg| AssertKind::WithMessage(msg, is_true))
150 .or(Some(AssertKind::WithoutMessage(is_true)));