1 use crate::consts::{constant, Constant};
2 use crate::utils::paths;
3 use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, resolve_node, 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;
9 use syntax::source_map::symbol::LocalInternedString;
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 lint_assert_cb = |is_debug_assert: bool| {
38 if let ExprKind::Unary(_, ref lit) = e.kind {
39 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit) {
43 ASSERTIONS_ON_CONSTANTS,
45 "`assert!(true)` will be optimized out by the compiler",
48 } else if !is_debug_assert {
51 ASSERTIONS_ON_CONSTANTS,
53 "`assert!(false)` should probably be replaced",
54 "use `panic!()` or `unreachable!()`",
60 if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
61 if debug_assert_span.from_expansion() {
65 } else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
66 if assert_span.from_expansion() {
69 if let Some((panic_message, is_true)) = assert_with_message(&cx, e) {
73 ASSERTIONS_ON_CONSTANTS,
75 "`assert!(true)` will be optimized out by the compiler",
78 } else if panic_message.starts_with("assertion failed: ") {
81 ASSERTIONS_ON_CONSTANTS,
83 "`assert!(false)` should probably be replaced",
84 "use `panic!()` or `unreachable!()`",
89 ASSERTIONS_ON_CONSTANTS,
91 &format!("`assert!(false, \"{}\")` should probably be replaced", panic_message,),
93 "use `panic!(\"{}\")` or `unreachable!(\"{}\")`",
94 panic_message, panic_message,
103 /// Check if the expression matches
106 /// match { let _t = !c; _t } {
109 /// ::std::rt::begin_panic(message, _)
116 /// where `message` is a string literal and `c` is a constant bool.
118 /// TODO extend this to match anything as message not just string literals
120 /// Returns the `message` argument of `begin_panic` and the value of `c` which is the
121 /// first argument of `assert!`.
122 fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<(LocalInternedString, bool)> {
124 if let ExprKind::Match(ref expr, ref arms, _) = expr.kind;
125 // matches { let _t = expr; _t }
126 if let ExprKind::DropTemps(ref expr) = expr.kind;
127 if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind;
128 // bind the first argument of the `assert!` macro
129 if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, expr);
131 if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind;
132 if let ExprKind::Lit(ref lit) = lit_expr.kind;
133 if let LitKind::Bool(true) = lit.node;
135 if let ExprKind::Block(ref block, _) = arms[0].body.kind;
136 if block.stmts.len() == 0;
137 if let Some(block_expr) = &block.expr;
138 if let ExprKind::Block(ref inner_block, _) = block_expr.kind;
139 if let Some(begin_panic_call) = &inner_block.expr;
141 if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
143 if let ExprKind::Lit(ref lit) = args[0].kind;
144 if let LitKind::Str(ref s, _) = lit.node;
145 // bind the second argument of the `assert!` macro
146 let panic_message = s.as_str();
147 // second argument of begin_panic is irrelevant
148 // as is the second match arm
150 return Some((panic_message, is_true));
156 /// Matches a function call with the given path and returns the arguments.
161 /// if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
163 fn match_function_call<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, path: &[&str]) -> Option<&'a [Expr]> {
165 if let ExprKind::Call(ref fun, ref args) = expr.kind;
166 if let ExprKind::Path(ref qpath) = fun.kind;
167 if let Some(fun_def_id) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
168 if match_def_path(cx, fun_def_id, path);