2019-10-05 09:45:02 -05:00
|
|
|
use crate::consts::{constant, Constant};
|
2019-10-06 13:10:30 -05:00
|
|
|
use crate::utils::paths;
|
2020-01-26 19:56:22 -06:00
|
|
|
use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, snippet_opt, span_lint_and_help};
|
2019-10-05 09:45:02 -05:00
|
|
|
use if_chain::if_chain;
|
2020-02-21 02:39:38 -06:00
|
|
|
use rustc_hir::{Expr, ExprKind, PatKind, UnOp};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-10-05 09:45:02 -05:00
|
|
|
use syntax::ast::LitKind;
|
2018-12-25 16:29:03 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-01-30 19:15:29 -06:00
|
|
|
/// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
|
|
|
|
/// panic!() or unreachable!()
|
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-10 17:01:56 -05:00
|
|
|
/// ```rust,ignore
|
2019-01-30 19:15:29 -06:00
|
|
|
/// assert!(false)
|
2019-03-05 10:50:33 -06:00
|
|
|
/// // or
|
2019-01-30 19:15:29 -06:00
|
|
|
/// assert!(true)
|
2019-03-05 10:50:33 -06:00
|
|
|
/// // or
|
|
|
|
/// const B: bool = false;
|
2019-01-30 19:15:29 -06:00
|
|
|
/// assert!(B)
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2019-01-09 04:38:38 -06:00
|
|
|
pub ASSERTIONS_ON_CONSTANTS,
|
|
|
|
style,
|
2019-03-10 12:19:47 -05:00
|
|
|
"`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
|
2018-12-25 16:29:03 -06:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
|
2018-12-25 16:29:03 -06:00
|
|
|
|
2019-01-09 04:38:38 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
|
2019-12-27 01:12:26 -06:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
|
2020-01-19 20:52:58 -06:00
|
|
|
let lint_true = |is_debug: bool| {
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2019-10-07 15:08:00 -05:00
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
2020-01-19 20:52:58 -06:00
|
|
|
if is_debug {
|
|
|
|
"`debug_assert!(true)` will be optimized out by the compiler"
|
|
|
|
} else {
|
|
|
|
"`assert!(true)` will be optimized out by the compiler"
|
|
|
|
},
|
2019-10-07 15:08:00 -05:00
|
|
|
"remove it",
|
|
|
|
);
|
|
|
|
};
|
|
|
|
let lint_false_without_message = || {
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2019-10-07 15:08:00 -05:00
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
|
|
|
"`assert!(false)` should probably be replaced",
|
|
|
|
"use `panic!()` or `unreachable!()`",
|
|
|
|
);
|
2019-08-17 13:46:44 -05:00
|
|
|
};
|
2019-10-07 15:08:00 -05:00
|
|
|
let lint_false_with_message = |panic_message: String| {
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2019-10-07 15:08:00 -05:00
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
|
|
|
&format!("`assert!(false, {})` should probably be replaced", panic_message),
|
|
|
|
&format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2019-08-17 13:46:44 -05:00
|
|
|
if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
|
2019-08-19 11:30:32 -05:00
|
|
|
if debug_assert_span.from_expansion() {
|
2019-08-17 13:46:44 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-10-07 15:08:00 -05:00
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Unary(_, ref lit) = e.kind;
|
|
|
|
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit);
|
|
|
|
if is_true;
|
|
|
|
then {
|
2020-01-19 20:52:58 -06:00
|
|
|
lint_true(true);
|
2019-10-07 15:08:00 -05:00
|
|
|
}
|
|
|
|
};
|
2019-08-17 13:46:44 -05:00
|
|
|
} else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
|
2019-08-19 11:30:32 -05:00
|
|
|
if assert_span.from_expansion() {
|
2019-08-17 13:46:44 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-10-07 15:08:00 -05:00
|
|
|
if let Some(assert_match) = match_assert_with_message(&cx, e) {
|
|
|
|
match assert_match {
|
|
|
|
// matched assert but not message
|
|
|
|
AssertKind::WithoutMessage(false) => lint_false_without_message(),
|
2020-01-19 20:52:58 -06:00
|
|
|
AssertKind::WithoutMessage(true) | AssertKind::WithMessage(_, true) => lint_true(false),
|
2019-10-07 15:08:00 -05:00
|
|
|
AssertKind::WithMessage(panic_message, false) => lint_false_with_message(panic_message),
|
|
|
|
};
|
2019-10-05 09:45:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 15:08:00 -05:00
|
|
|
/// Result of calling `match_assert_with_message`.
|
|
|
|
enum AssertKind {
|
|
|
|
WithMessage(String, bool),
|
|
|
|
WithoutMessage(bool),
|
|
|
|
}
|
|
|
|
|
2019-10-06 13:10:30 -05:00
|
|
|
/// Check if the expression matches
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// match { let _t = !c; _t } {
|
|
|
|
/// true => {
|
|
|
|
/// {
|
|
|
|
/// ::std::rt::begin_panic(message, _)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// _ => { }
|
|
|
|
/// };
|
|
|
|
/// ```
|
|
|
|
///
|
2019-10-07 15:08:00 -05:00
|
|
|
/// where `message` is any expression and `c` is a constant bool.
|
2019-12-27 01:12:26 -06:00
|
|
|
fn match_assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) -> Option<AssertKind> {
|
2019-10-05 09:45:02 -05:00
|
|
|
if_chain! {
|
2019-10-06 13:10:30 -05:00
|
|
|
if let ExprKind::Match(ref expr, ref arms, _) = expr.kind;
|
|
|
|
// matches { let _t = expr; _t }
|
2019-10-05 09:45:02 -05:00
|
|
|
if let ExprKind::DropTemps(ref expr) = expr.kind;
|
|
|
|
if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind;
|
2019-10-06 13:10:30 -05:00
|
|
|
// bind the first argument of the `assert!` macro
|
2019-10-05 09:45:02 -05:00
|
|
|
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, expr);
|
|
|
|
// arm 1 pattern
|
|
|
|
if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind;
|
|
|
|
if let ExprKind::Lit(ref lit) = lit_expr.kind;
|
|
|
|
if let LitKind::Bool(true) = lit.node;
|
|
|
|
// arm 1 block
|
2019-10-06 13:10:30 -05:00
|
|
|
if let ExprKind::Block(ref block, _) = arms[0].body.kind;
|
2019-12-27 01:12:26 -06:00
|
|
|
if block.stmts.is_empty();
|
2019-10-06 13:10:30 -05:00
|
|
|
if let Some(block_expr) = &block.expr;
|
|
|
|
if let ExprKind::Block(ref inner_block, _) = block_expr.kind;
|
|
|
|
if let Some(begin_panic_call) = &inner_block.expr;
|
2019-10-05 09:45:02 -05:00
|
|
|
// function call
|
2019-10-06 13:10:30 -05:00
|
|
|
if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
|
2020-01-05 00:43:41 -06:00
|
|
|
if args.len() == 1;
|
2019-10-07 15:08:00 -05:00
|
|
|
// bind the second argument of the `assert!` macro if it exists
|
|
|
|
if let panic_message = snippet_opt(cx, args[0].span);
|
2019-10-06 13:10:30 -05:00
|
|
|
// second argument of begin_panic is irrelevant
|
|
|
|
// as is the second match arm
|
2019-10-05 09:45:02 -05:00
|
|
|
then {
|
2019-10-07 15:08:00 -05:00
|
|
|
// an empty message occurs when it was generated by the macro
|
|
|
|
// (and not passed by the user)
|
|
|
|
return panic_message
|
|
|
|
.filter(|msg| !msg.is_empty())
|
|
|
|
.map(|msg| AssertKind::WithMessage(msg, is_true))
|
|
|
|
.or(Some(AssertKind::WithoutMessage(is_true)));
|
2018-12-25 16:29:03 -06:00
|
|
|
}
|
|
|
|
}
|
2019-10-06 13:10:30 -05:00
|
|
|
None
|
|
|
|
}
|