rust-clippy/clippy_lints/src/assertions_on_constants.rs

146 lines
5.3 KiB
Rust
Raw Normal View History

use crate::consts::{constant, Constant};
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::source::snippet_opt;
use clippy_utils::{is_direct_expn_of, is_expn_of, match_panic_call};
use if_chain::if_chain;
2021-01-01 18:38:11 +00:00
use rustc_hir::{Expr, ExprKind, UnOp};
2020-01-12 06:08:41 +00:00
use rustc_lint::{LateContext, LateLintPass};
2020-01-11 11:37:08 +00:00
use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! {
2019-01-31 01:15:29 +00:00
/// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
///
/// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
2020-03-18 01:50:39 +00:00
/// `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)
/// assert!(true)
/// const B: bool = false;
2019-01-31 01:15:29 +00:00
/// assert!(B)
/// ```
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!()`"
}
2019-04-08 20:43:55 +00:00
declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
let lint_true = |is_debug: bool| {
span_lint_and_help(
2019-10-07 20:08:00 +00:00
cx,
ASSERTIONS_ON_CONSTANTS,
e.span,
if is_debug {
"`debug_assert!(true)` will be optimized out by the compiler"
} else {
"`assert!(true)` will be optimized out by the compiler"
},
None,
2019-10-07 20:08:00 +00:00
"remove it",
);
};
let lint_false_without_message = || {
span_lint_and_help(
2019-10-07 20:08:00 +00:00
cx,
ASSERTIONS_ON_CONSTANTS,
e.span,
"`assert!(false)` should probably be replaced",
None,
2019-10-07 20:08:00 +00:00
"use `panic!()` or `unreachable!()`",
);
2019-08-17 18:46:44 +00:00
};
2019-10-07 20:08:00 +00:00
let lint_false_with_message = |panic_message: String| {
span_lint_and_help(
2019-10-07 20:08:00 +00:00
cx,
ASSERTIONS_ON_CONSTANTS,
e.span,
&format!("`assert!(false, {})` should probably be replaced", panic_message),
None,
2019-10-07 20:08:00 +00:00
&format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message),
)
};
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;
}
2019-10-07 20:08:00 +00:00
if_chain! {
if let ExprKind::Unary(_, lit) = e.kind;
2020-07-17 08:47:04 +00:00
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), lit);
2019-10-07 20:08:00 +00:00
if is_true;
then {
lint_true(true);
2019-10-07 20:08:00 +00:00
}
};
2019-08-17 18:46:44 +00:00
} 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;
}
if let Some(assert_match) = match_assert_with_message(cx, e) {
2019-10-07 20:08:00 +00:00
match assert_match {
// matched assert but not message
AssertKind::WithoutMessage(false) => lint_false_without_message(),
AssertKind::WithoutMessage(true) | AssertKind::WithMessage(_, true) => lint_true(false),
2019-10-07 20:08:00 +00:00
AssertKind::WithMessage(panic_message, false) => lint_false_with_message(panic_message),
};
}
}
}
}
2019-10-07 20:08:00 +00:00
/// Result of calling `match_assert_with_message`.
enum AssertKind {
WithMessage(String, bool),
WithoutMessage(bool),
}
2019-10-06 18:10:30 +00:00
/// Check if the expression matches
///
/// ```rust,ignore
2021-01-01 18:38:11 +00:00
/// if !c {
/// {
/// ::std::rt::begin_panic(message, _)
/// }
/// }
2019-10-06 18:10:30 +00:00
/// ```
///
2019-10-07 20:08:00 +00:00
/// where `message` is any expression and `c` is a constant bool.
fn match_assert_with_message<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<AssertKind> {
if_chain! {
if let ExprKind::If(cond, then, _) = expr.kind;
if let ExprKind::Unary(UnOp::Not, expr) = cond.kind;
2019-10-06 18:10:30 +00:00
// bind the first argument of the `assert!` macro
2020-07-17 08:47:04 +00:00
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), expr);
2021-01-01 18:38:11 +00:00
// block
if let ExprKind::Block(block, _) = then.kind;
2019-12-27 07:12:26 +00:00
if block.stmts.is_empty();
2019-10-06 18:10:30 +00:00
if let Some(block_expr) = &block.expr;
// inner block is optional. unwrap it if it exists, or use the expression as is otherwise.
if let Some(begin_panic_call) = match block_expr.kind {
ExprKind::Block(inner_block, _) => &inner_block.expr,
_ => &block.expr,
};
// function call
if let Some(arg) = match_panic_call(cx, begin_panic_call);
2019-10-07 20:08:00 +00:00
// bind the second argument of the `assert!` macro if it exists
if let panic_message = snippet_opt(cx, arg.span);
2019-10-06 18:10:30 +00:00
// second argument of begin_panic is irrelevant
// as is the second match arm
then {
2019-10-07 20:08:00 +00: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)));
}
}
2019-10-06 18:10:30 +00:00
None
}