2022-10-06 02:44:38 -05:00
|
|
|
use crate::rustc_lint::LintContext;
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2023-02-10 07:01:19 -06:00
|
|
|
use clippy_utils::macros::root_macro_call;
|
2022-12-17 07:12:54 -06:00
|
|
|
use clippy_utils::{is_else_clause, peel_blocks_with_stmt, span_extract_comment, sugg};
|
2021-09-28 12:03:12 -05:00
|
|
|
use rustc_errors::Applicability;
|
2022-01-13 06:18:19 -06:00
|
|
|
use rustc_hir::{Expr, ExprKind, UnOp};
|
2021-09-28 12:03:12 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2022-01-13 06:18:19 -06:00
|
|
|
use rustc_span::sym;
|
2021-09-28 12:03:12 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Detects `if`-then-`panic!` that can be replaced with `assert!`.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// `assert!` is simpler than `if`-then-`panic!`.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
/// ```rust
|
|
|
|
/// let sad_people: Vec<&str> = vec![];
|
|
|
|
/// if !sad_people.is_empty() {
|
|
|
|
/// panic!("there are sad people: {:?}", sad_people);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// let sad_people: Vec<&str> = vec![];
|
|
|
|
/// assert!(sad_people.is_empty(), "there are sad people: {:?}", sad_people);
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.57.0"]
|
2021-11-04 07:52:36 -05:00
|
|
|
pub MANUAL_ASSERT,
|
|
|
|
pedantic,
|
2021-09-28 12:03:12 -05:00
|
|
|
"`panic!` and only a `panic!` in `if`-then statement"
|
|
|
|
}
|
|
|
|
|
2021-11-04 07:52:36 -05:00
|
|
|
declare_lint_pass!(ManualAssert => [MANUAL_ASSERT]);
|
2021-09-28 12:03:12 -05:00
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for ManualAssert {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
|
2023-02-10 07:01:19 -06:00
|
|
|
if let ExprKind::If(cond, then, None) = expr.kind
|
|
|
|
&& !matches!(cond.kind, ExprKind::Let(_))
|
|
|
|
&& !expr.span.from_expansion()
|
|
|
|
&& let then = peel_blocks_with_stmt(then)
|
|
|
|
&& let Some(macro_call) = root_macro_call(then.span)
|
|
|
|
&& cx.tcx.item_name(macro_call.def_id) == sym::panic
|
|
|
|
&& !cx.tcx.sess.source_map().is_multiline(cond.span)
|
|
|
|
&& let Ok(panic_snippet) = cx.sess().source_map().span_to_snippet(macro_call.span)
|
|
|
|
&& let Some(panic_snippet) = panic_snippet.strip_suffix(')')
|
|
|
|
&& let Some((_, format_args_snip)) = panic_snippet.split_once('(')
|
2022-12-17 07:12:54 -06:00
|
|
|
// Don't change `else if foo { panic!(..) }` to `else { assert!(foo, ..) }` as it just
|
|
|
|
// shuffles the condition around.
|
|
|
|
// Should this have a config value?
|
2023-02-10 07:01:19 -06:00
|
|
|
&& !is_else_clause(cx.tcx, expr)
|
|
|
|
{
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let cond = cond.peel_drop_temps();
|
|
|
|
let mut comments = span_extract_comment(cx.sess().source_map(), expr.span);
|
|
|
|
if !comments.is_empty() {
|
|
|
|
comments += "\n";
|
2021-09-28 12:03:12 -05:00
|
|
|
}
|
2023-02-10 07:01:19 -06:00
|
|
|
let (cond, not) = match cond.kind {
|
|
|
|
ExprKind::Unary(UnOp::Not, e) => (e, ""),
|
|
|
|
_ => (cond, "!"),
|
|
|
|
};
|
|
|
|
let cond_sugg = sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par();
|
|
|
|
let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip});");
|
2023-05-05 10:45:49 -05:00
|
|
|
// we show to the user the suggestion without the comments, but when applying the fix, include the comments in the block
|
2023-02-10 07:01:19 -06:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
MANUAL_ASSERT,
|
|
|
|
expr.span,
|
|
|
|
"only a `panic!` in `if`-then statement",
|
|
|
|
|diag| {
|
|
|
|
// comments can be noisy, do not show them to the user
|
|
|
|
if !comments.is_empty() {
|
|
|
|
diag.tool_only_span_suggestion(
|
|
|
|
expr.span.shrink_to_lo(),
|
|
|
|
"add comments back",
|
|
|
|
comments,
|
|
|
|
applicability
|
|
|
|
);
|
|
|
|
}
|
|
|
|
diag.span_suggestion(
|
|
|
|
expr.span,
|
|
|
|
"try instead",
|
|
|
|
sugg,
|
|
|
|
applicability
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2021-09-28 12:03:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|