2021-09-14 16:28:09 +08:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::higher::PanicExpn;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2021-10-01 13:13:09 +08:00
|
|
|
use clippy_utils::{is_expn_of, sugg};
|
2021-09-14 16:28:09 +08:00
|
|
|
use rustc_errors::Applicability;
|
2021-10-01 13:13:09 +08:00
|
|
|
use rustc_hir::{Block, Expr, ExprKind, StmtKind, UnOp};
|
2021-09-14 16:28:09 +08:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
|
|
|
|
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);
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 21:06:26 +02:00
|
|
|
#[clippy::version = "1.57.0"]
|
2021-10-12 09:23:05 -05:00
|
|
|
pub MANUAL_ASSERT,
|
2021-10-12 09:09:54 -05:00
|
|
|
pedantic,
|
2021-09-14 16:28:09 +08:00
|
|
|
"`panic!` and only a `panic!` in `if`-then statement"
|
|
|
|
}
|
|
|
|
|
2021-10-12 09:23:05 -05:00
|
|
|
declare_lint_pass!(ManualAssert => [MANUAL_ASSERT]);
|
2021-09-14 16:28:09 +08:00
|
|
|
|
2021-10-12 09:23:05 -05:00
|
|
|
impl LateLintPass<'_> for ManualAssert {
|
2021-09-14 16:28:09 +08:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|
|
|
if_chain! {
|
|
|
|
if let Expr {
|
|
|
|
kind: ExprKind:: If(cond, Expr {
|
|
|
|
kind: ExprKind::Block(
|
|
|
|
Block {
|
|
|
|
stmts: [stmt],
|
|
|
|
..
|
|
|
|
},
|
|
|
|
_),
|
|
|
|
..
|
|
|
|
}, None),
|
|
|
|
..
|
|
|
|
} = &expr;
|
|
|
|
if is_expn_of(stmt.span, "panic").is_some();
|
|
|
|
if !matches!(cond.kind, ExprKind::Let(_, _, _));
|
|
|
|
if let StmtKind::Semi(semi) = stmt.kind;
|
|
|
|
if !cx.tcx.sess.source_map().is_multiline(cond.span);
|
|
|
|
|
|
|
|
then {
|
2021-10-21 00:08:18 +01:00
|
|
|
let call = if_chain! {
|
|
|
|
if let ExprKind::Block(block, _) = semi.kind;
|
|
|
|
if let Some(init) = block.expr;
|
|
|
|
then {
|
|
|
|
init
|
|
|
|
} else {
|
|
|
|
semi
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let span = if let Some(panic_expn) = PanicExpn::parse(call) {
|
2021-09-14 16:28:09 +08:00
|
|
|
match *panic_expn.format_args.value_args {
|
|
|
|
[] => panic_expn.format_args.format_string_span,
|
|
|
|
[.., last] => panic_expn.format_args.format_string_span.to(last.span),
|
|
|
|
}
|
2021-10-21 00:08:18 +01:00
|
|
|
} else if let ExprKind::Call(_, [format_args]) = call.kind {
|
|
|
|
format_args.span
|
2021-09-14 16:28:09 +08:00
|
|
|
} else {
|
2021-10-21 00:08:18 +01:00
|
|
|
return
|
2021-09-14 16:28:09 +08:00
|
|
|
};
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let sugg = snippet_with_applicability(cx, span, "..", &mut applicability);
|
2021-09-30 19:06:42 +08:00
|
|
|
let cond_sugg = if let ExprKind::DropTemps(e, ..) = cond.kind {
|
|
|
|
if let Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..} = e {
|
2021-10-01 13:13:09 +08:00
|
|
|
sugg::Sugg::hir_with_applicability(cx, not_expr, "..", &mut applicability).maybe_par().to_string()
|
2021-09-30 19:06:42 +08:00
|
|
|
} else {
|
2021-09-30 07:38:03 -04:00
|
|
|
format!("!{}", sugg::Sugg::hir_with_applicability(cx, e, "..", &mut applicability).maybe_par())
|
2021-09-30 19:06:42 +08:00
|
|
|
}
|
2021-09-14 16:28:09 +08:00
|
|
|
} else {
|
2021-09-30 07:38:03 -04:00
|
|
|
format!("!{}", sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par())
|
2021-09-14 16:28:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2021-10-12 09:23:05 -05:00
|
|
|
MANUAL_ASSERT,
|
2021-09-14 16:28:09 +08:00
|
|
|
expr.span,
|
|
|
|
"only a `panic!` in `if`-then statement",
|
|
|
|
"try",
|
|
|
|
format!("assert!({}, {});", cond_sugg, sugg),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|