2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
|
|
|
use clippy_utils::source::snippet_with_macro_callsite;
|
2021-11-29 14:34:36 +00:00
|
|
|
use clippy_utils::{contains_return, higher, is_else_clause, is_lang_ctor, meets_msrv, msrvs, peel_blocks};
|
2021-03-25 18:29:11 +00:00
|
|
|
use if_chain::if_chain;
|
2021-04-22 09:31:13 +00:00
|
|
|
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
2021-11-16 05:39:52 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind, Stmt, StmtKind};
|
2021-03-25 18:29:11 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_middle::lint::in_external_macro;
|
|
|
|
use rustc_semver::RustcVersion;
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for if-else that could be written to `bool::then`.
|
2021-03-25 18:29:11 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Looks a little redundant. Using `bool::then` helps it have less lines of code.
|
2021-03-25 18:29:11 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2021-03-25 18:29:11 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # let v = vec![0];
|
|
|
|
/// let a = if v.is_empty() {
|
|
|
|
/// println!("true!");
|
|
|
|
/// Some(42)
|
|
|
|
/// } else {
|
|
|
|
/// None
|
|
|
|
/// };
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # let v = vec![0];
|
|
|
|
/// let a = v.is_empty().then(|| {
|
|
|
|
/// println!("true!");
|
|
|
|
/// 42
|
|
|
|
/// });
|
|
|
|
/// ```
|
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 19:06:26 +00:00
|
|
|
#[clippy::version = "1.53.0"]
|
2021-03-25 18:29:11 +00:00
|
|
|
pub IF_THEN_SOME_ELSE_NONE,
|
|
|
|
restriction,
|
|
|
|
"Finds if-else that could be written using `bool::then`"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IfThenSomeElseNone {
|
|
|
|
msrv: Option<RustcVersion>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IfThenSomeElseNone {
|
|
|
|
#[must_use]
|
|
|
|
pub fn new(msrv: Option<RustcVersion>) -> Self {
|
|
|
|
Self { msrv }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]);
|
|
|
|
|
2022-01-11 15:52:23 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
|
2021-03-25 18:29:11 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
|
2022-05-08 11:13:14 +00:00
|
|
|
if !meets_msrv(self.msrv, msrvs::BOOL_THEN) {
|
2021-03-25 18:29:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if in_external_macro(cx.sess(), expr.span) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only care about the top-most `if` in the chain
|
2021-04-22 09:31:13 +00:00
|
|
|
if is_else_clause(cx.tcx, expr) {
|
2021-03-25 18:29:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if_chain! {
|
2021-08-08 14:49:13 +00:00
|
|
|
if let Some(higher::If { cond, then, r#else: Some(els) }) = higher::If::hir(expr);
|
2021-04-08 15:50:13 +00:00
|
|
|
if let ExprKind::Block(then_block, _) = then.kind;
|
|
|
|
if let Some(then_expr) = then_block.expr;
|
|
|
|
if let ExprKind::Call(then_call, [then_arg]) = then_expr.kind;
|
2021-03-25 18:29:11 +00:00
|
|
|
if let ExprKind::Path(ref then_call_qpath) = then_call.kind;
|
2021-04-22 09:31:13 +00:00
|
|
|
if is_lang_ctor(cx, then_call_qpath, OptionSome);
|
2021-11-29 14:34:36 +00:00
|
|
|
if let ExprKind::Path(ref qpath) = peel_blocks(els).kind;
|
2021-04-22 09:31:13 +00:00
|
|
|
if is_lang_ctor(cx, qpath, OptionNone);
|
2021-11-16 05:39:52 +00:00
|
|
|
if !stmts_contains_early_return(then_block.stmts);
|
2021-03-25 18:29:11 +00:00
|
|
|
then {
|
|
|
|
let cond_snip = snippet_with_macro_callsite(cx, cond.span, "[condition]");
|
|
|
|
let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) {
|
|
|
|
format!("({})", cond_snip)
|
|
|
|
} else {
|
|
|
|
cond_snip.into_owned()
|
|
|
|
};
|
|
|
|
let arg_snip = snippet_with_macro_callsite(cx, then_arg.span, "");
|
|
|
|
let closure_body = if then_block.stmts.is_empty() {
|
|
|
|
arg_snip.into_owned()
|
|
|
|
} else {
|
|
|
|
format!("{{ /* snippet */ {} }}", arg_snip)
|
|
|
|
};
|
|
|
|
let help = format!(
|
|
|
|
"consider using `bool::then` like: `{}.then(|| {})`",
|
|
|
|
cond_snip,
|
|
|
|
closure_body,
|
|
|
|
);
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
IF_THEN_SOME_ELSE_NONE,
|
|
|
|
expr.span,
|
|
|
|
"this could be simplified with `bool::then`",
|
|
|
|
None,
|
|
|
|
&help,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extract_msrv_attr!(LateContext);
|
|
|
|
}
|
2021-11-16 05:39:52 +00:00
|
|
|
|
|
|
|
fn stmts_contains_early_return(stmts: &[Stmt<'_>]) -> bool {
|
|
|
|
stmts.iter().any(|stmt| {
|
|
|
|
let Stmt { kind: StmtKind::Semi(e), .. } = stmt else { return false };
|
|
|
|
|
|
|
|
contains_return(e)
|
|
|
|
})
|
|
|
|
}
|