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_help_and_lint};
4 use if_chain::if_chain;
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::{declare_lint_pass, declare_tool_lint};
8 use syntax::ast::LitKind;
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
24 /// const B: bool = false;
27 pub ASSERTIONS_ON_CONSTANTS,
29 "`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
32 declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
34 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
35 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
39 ASSERTIONS_ON_CONSTANTS,
41 "`assert!(true)` will be optimized out by the compiler",
45 let lint_false_without_message = || {
48 ASSERTIONS_ON_CONSTANTS,
50 "`assert!(false)` should probably be replaced",
51 "use `panic!()` or `unreachable!()`",
54 let lint_false_with_message = |panic_message: String| {
57 ASSERTIONS_ON_CONSTANTS,
59 &format!("`assert!(false, {})` should probably be replaced", panic_message),
60 &format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message),
64 if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
65 if debug_assert_span.from_expansion() {
69 if let ExprKind::Unary(_, ref lit) = e.kind;
70 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit);
76 } else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
77 if assert_span.from_expansion() {
80 if let Some(assert_match) = match_assert_with_message(&cx, e) {
82 // matched assert but not message
83 AssertKind::WithoutMessage(false) => lint_false_without_message(),
84 AssertKind::WithoutMessage(true) | AssertKind::WithMessage(_, true) => lint_true(),
85 AssertKind::WithMessage(panic_message, false) => lint_false_with_message(panic_message),
92 /// Result of calling `match_assert_with_message`.
94 WithMessage(String, bool),
98 /// Check if the expression matches
101 /// match { let _t = !c; _t } {
104 /// ::std::rt::begin_panic(message, _)
111 /// where `message` is any expression and `c` is a constant bool.
112 fn match_assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<AssertKind> {
114 if let ExprKind::Match(ref expr, ref arms, _) = expr.kind;
115 // matches { let _t = expr; _t }
116 if let ExprKind::DropTemps(ref expr) = expr.kind;
117 if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind;
118 // bind the first argument of the `assert!` macro
119 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, expr);
121 if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind;
122 if let ExprKind::Lit(ref lit) = lit_expr.kind;
123 if let LitKind::Bool(true) = lit.node;
125 if let ExprKind::Block(ref block, _) = arms[0].body.kind;
126 if block.stmts.len() == 0;
127 if let Some(block_expr) = &block.expr;
128 if let ExprKind::Block(ref inner_block, _) = block_expr.kind;
129 if let Some(begin_panic_call) = &inner_block.expr;
131 if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
133 // bind the second argument of the `assert!` macro if it exists
134 if let panic_message = snippet_opt(cx, args[0].span);
135 // second argument of begin_panic is irrelevant
136 // as is the second match arm
138 // an empty message occurs when it was generated by the macro
139 // (and not passed by the user)
141 .filter(|msg| !msg.is_empty())
142 .map(|msg| AssertKind::WithMessage(msg, is_true))
143 .or(Some(AssertKind::WithoutMessage(is_true)));