2019-10-05 14:45:02 +00:00
|
|
|
use crate::consts::{constant, Constant};
|
2019-10-06 18:10:30 +00:00
|
|
|
use crate::utils::paths;
|
2019-10-07 18:40:05 +00:00
|
|
|
use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, span_help_and_lint, snippet};
|
2019-10-05 14:45:02 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc::hir::*;
|
2019-01-31 01:15:29 +00:00
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2019-04-08 20:43:55 +00:00
|
|
|
use rustc::{declare_lint_pass, declare_tool_lint};
|
2019-10-05 14:45:02 +00:00
|
|
|
use syntax::ast::LitKind;
|
2019-10-07 18:40:05 +00:00
|
|
|
use std::borrow::Cow;
|
2018-12-25 22:29:03 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-01-31 01:15:29 +00:00
|
|
|
/// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
|
2019-03-05 16:50:33 +00: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 22:01:56 +00:00
|
|
|
/// ```rust,ignore
|
2019-01-31 01:15:29 +00:00
|
|
|
/// assert!(false)
|
2019-03-05 16:50:33 +00:00
|
|
|
/// // or
|
2019-01-31 01:15:29 +00:00
|
|
|
/// assert!(true)
|
2019-03-05 16:50:33 +00:00
|
|
|
/// // or
|
|
|
|
/// const B: bool = false;
|
2019-01-31 01:15:29 +00:00
|
|
|
/// assert!(B)
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2019-01-09 10:38:38 +00:00
|
|
|
pub ASSERTIONS_ON_CONSTANTS,
|
|
|
|
style,
|
2019-03-10 17:19:47 +00: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 22:29:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
|
2018-12-25 22:29:03 +00:00
|
|
|
|
2019-01-09 10:38:38 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
|
2018-12-25 22:29:03 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
|
2019-08-17 18:46:44 +00:00
|
|
|
let lint_assert_cb = |is_debug_assert: bool| {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Unary(_, ref lit) = e.kind {
|
2019-08-17 18:46:44 +00:00
|
|
|
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit) {
|
|
|
|
if is_true {
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
|
|
|
"`assert!(true)` will be optimized out by the compiler",
|
|
|
|
"remove it",
|
|
|
|
);
|
|
|
|
} else if !is_debug_assert {
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
|
|
|
"`assert!(false)` should probably be replaced",
|
|
|
|
"use `panic!()` or `unreachable!()`",
|
|
|
|
);
|
2019-08-17 18:46:44 +00:00
|
|
|
}
|
2018-12-25 22:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-17 18:46:44 +00:00
|
|
|
};
|
|
|
|
if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
|
2019-08-19 16:30:32 +00:00
|
|
|
if debug_assert_span.from_expansion() {
|
2019-08-17 18:46:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
lint_assert_cb(true);
|
|
|
|
} else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
|
2019-08-19 16:30:32 +00:00
|
|
|
if assert_span.from_expansion() {
|
2019-08-17 18:46:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-10-05 14:45:02 +00:00
|
|
|
if let Some((panic_message, is_true)) = assert_with_message(&cx, e) {
|
|
|
|
if is_true {
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
|
|
|
"`assert!(true)` will be optimized out by the compiler",
|
|
|
|
"remove it",
|
|
|
|
);
|
2019-10-07 18:40:05 +00:00
|
|
|
} else if panic_message.is_empty() || panic_message.starts_with("\"assertion failed: ") {
|
2019-10-05 14:45:02 +00:00
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
|
|
|
"`assert!(false)` should probably be replaced",
|
|
|
|
"use `panic!()` or `unreachable!()`",
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
2019-10-07 18:40:05 +00:00
|
|
|
&format!("`assert!(false, {})` should probably be replaced", panic_message,),
|
2019-10-05 14:45:02 +00:00
|
|
|
&format!(
|
2019-10-07 18:40:05 +00:00
|
|
|
"use `panic!({})` or `unreachable!({})`",
|
2019-10-05 14:45:02 +00:00
|
|
|
panic_message, panic_message,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-06 18:10:30 +00:00
|
|
|
/// Check if the expression matches
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// match { let _t = !c; _t } {
|
|
|
|
/// true => {
|
|
|
|
/// {
|
|
|
|
/// ::std::rt::begin_panic(message, _)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// _ => { }
|
|
|
|
/// };
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// where `message` is a string literal and `c` is a constant bool.
|
|
|
|
///
|
|
|
|
/// TODO extend this to match anything as message not just string literals
|
|
|
|
///
|
|
|
|
/// Returns the `message` argument of `begin_panic` and the value of `c` which is the
|
|
|
|
/// first argument of `assert!`.
|
2019-10-07 18:40:05 +00:00
|
|
|
fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<(Cow<'a, str>, bool)> {
|
2019-10-05 14:45:02 +00:00
|
|
|
if_chain! {
|
2019-10-06 18:10:30 +00:00
|
|
|
if let ExprKind::Match(ref expr, ref arms, _) = expr.kind;
|
|
|
|
// matches { let _t = expr; _t }
|
2019-10-05 14:45:02 +00:00
|
|
|
if let ExprKind::DropTemps(ref expr) = expr.kind;
|
|
|
|
if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind;
|
2019-10-06 18:10:30 +00:00
|
|
|
// bind the first argument of the `assert!` macro
|
2019-10-05 14:45:02 +00: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 18:10:30 +00:00
|
|
|
if let ExprKind::Block(ref block, _) = arms[0].body.kind;
|
|
|
|
if block.stmts.len() == 0;
|
|
|
|
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 14:45:02 +00:00
|
|
|
// function call
|
2019-10-06 18:10:30 +00:00
|
|
|
if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
|
2019-10-05 14:45:02 +00:00
|
|
|
if args.len() == 2;
|
2019-10-06 18:10:30 +00:00
|
|
|
// bind the second argument of the `assert!` macro
|
2019-10-07 18:40:05 +00:00
|
|
|
let panic_message_arg = snippet(cx, args[0].span, "..");
|
2019-10-06 18:10:30 +00:00
|
|
|
// second argument of begin_panic is irrelevant
|
|
|
|
// as is the second match arm
|
2019-10-05 14:45:02 +00:00
|
|
|
then {
|
2019-10-07 18:40:05 +00:00
|
|
|
return Some((panic_message_arg, is_true));
|
2018-12-25 22:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-06 18:10:30 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Matches a function call with the given path and returns the arguments.
|
|
|
|
///
|
|
|
|
/// Usage:
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
|
|
|
|
/// ```
|
|
|
|
fn match_function_call<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, path: &[&str]) -> Option<&'a [Expr]> {
|
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Call(ref fun, ref args) = expr.kind;
|
|
|
|
if let ExprKind::Path(ref qpath) = fun.kind;
|
2019-10-07 18:40:05 +00:00
|
|
|
if let Some(fun_def_id) = cx.tables.qpath_res(qpath, fun.hir_id).opt_def_id();
|
2019-10-06 18:10:30 +00:00
|
|
|
if match_def_path(cx, fun_def_id, path);
|
|
|
|
then {
|
|
|
|
return Some(&args)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
None
|
2018-12-25 22:29:03 +00:00
|
|
|
}
|