1 use crate::consts::{constant, Constant};
2 use crate::utils::paths;
3 use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, snippet_opt, span_lint_and_help};
4 use if_chain::if_chain;
5 use rustc_ast::ast::LitKind;
6 use rustc_hir::{Expr, ExprKind, PatKind, UnOp};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
11 /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
13 /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
14 /// `panic!()` or `unreachable!()`
16 /// **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_true = |is_debug: bool| {
37 ASSERTIONS_ON_CONSTANTS,
40 "`debug_assert!(true)` will be optimized out by the compiler"
42 "`assert!(true)` will be optimized out by the compiler"
48 let lint_false_without_message = || {
51 ASSERTIONS_ON_CONSTANTS,
53 "`assert!(false)` should probably be replaced",
55 "use `panic!()` or `unreachable!()`",
58 let lint_false_with_message = |panic_message: String| {
61 ASSERTIONS_ON_CONSTANTS,
63 &format!("`assert!(false, {})` should probably be replaced", panic_message),
65 &format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message),
69 if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
70 if debug_assert_span.from_expansion() {
74 if let ExprKind::Unary(_, ref lit) = e.kind;
75 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit);
81 } else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
82 if assert_span.from_expansion() {
85 if let Some(assert_match) = match_assert_with_message(&cx, e) {
87 // matched assert but not message
88 AssertKind::WithoutMessage(false) => lint_false_without_message(),
89 AssertKind::WithoutMessage(true) | AssertKind::WithMessage(_, true) => lint_true(false),
90 AssertKind::WithMessage(panic_message, false) => lint_false_with_message(panic_message),
97 /// Result of calling `match_assert_with_message`.
99 WithMessage(String, bool),
100 WithoutMessage(bool),
103 /// Check if the expression matches
106 /// match { let _t = !c; _t } {
109 /// ::std::rt::begin_panic(message, _)
116 /// where `message` is any expression and `c` is a constant bool.
117 fn match_assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) -> Option<AssertKind> {
119 if let ExprKind::Match(ref expr, ref arms, _) = expr.kind;
120 // matches { let _t = expr; _t }
121 if let ExprKind::DropTemps(ref expr) = expr.kind;
122 if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind;
123 // bind the first argument of the `assert!` macro
124 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, expr);
126 if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind;
127 if let ExprKind::Lit(ref lit) = lit_expr.kind;
128 if let LitKind::Bool(true) = lit.node;
130 if let ExprKind::Block(ref block, _) = arms[0].body.kind;
131 if block.stmts.is_empty();
132 if let Some(block_expr) = &block.expr;
133 if let ExprKind::Block(ref inner_block, _) = block_expr.kind;
134 if let Some(begin_panic_call) = &inner_block.expr;
136 if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
138 // bind the second argument of the `assert!` macro if it exists
139 if let panic_message = snippet_opt(cx, args[0].span);
140 // second argument of begin_panic is irrelevant
141 // as is the second match arm
143 // an empty message occurs when it was generated by the macro
144 // (and not passed by the user)
146 .filter(|msg| !msg.is_empty())
147 .map(|msg| AssertKind::WithMessage(msg, is_true))
148 .or(Some(AssertKind::WithoutMessage(is_true)));