2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2021-03-14 23:17:44 +00:00
|
|
|
use clippy_utils::source::snippet_with_macro_callsite;
|
2021-04-14 17:40:51 +00:00
|
|
|
use clippy_utils::{is_else_clause, is_lang_ctor, meets_msrv};
|
2021-03-06 17:08:46 +00:00
|
|
|
use if_chain::if_chain;
|
2021-04-02 22:09:58 +00:00
|
|
|
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
2021-03-06 17:08:46 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_middle::lint::in_external_macro;
|
2021-03-08 13:52:03 +00:00
|
|
|
use rustc_semver::RustcVersion;
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
|
|
|
|
|
|
|
const IF_THEN_SOME_ELSE_NONE_MSRV: RustcVersion = RustcVersion::new(1, 50, 0);
|
2021-03-06 17:08:46 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for if-else that could be written to `bool::then`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Looks a little redundant. Using `bool::then` helps it have less lines of code.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```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
|
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
pub IF_THEN_SOME_ELSE_NONE,
|
|
|
|
restriction,
|
|
|
|
"Finds if-else that could be written using `bool::then`"
|
|
|
|
}
|
|
|
|
|
2021-03-08 13:52:03 +00:00
|
|
|
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]);
|
2021-03-06 17:08:46 +00:00
|
|
|
|
|
|
|
impl LateLintPass<'_> for IfThenSomeElseNone {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
|
2021-03-16 16:06:34 +00:00
|
|
|
if !meets_msrv(self.msrv.as_ref(), &IF_THEN_SOME_ELSE_NONE_MSRV) {
|
2021-03-08 13:52:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-06 17:08:46 +00:00
|
|
|
if in_external_macro(cx.sess(), expr.span) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only care about the top-most `if` in the chain
|
2021-04-14 17:40:51 +00:00
|
|
|
if is_else_clause(cx.tcx, expr) {
|
2021-03-06 17:08:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if_chain! {
|
2021-04-02 21:35:32 +00:00
|
|
|
if let ExprKind::If(cond, then, Some(els)) = expr.kind;
|
|
|
|
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-06 17:08:46 +00:00
|
|
|
if let ExprKind::Path(ref then_call_qpath) = then_call.kind;
|
2021-04-02 22:09:58 +00:00
|
|
|
if is_lang_ctor(cx, then_call_qpath, OptionSome);
|
2021-04-02 21:35:32 +00:00
|
|
|
if let ExprKind::Block(els_block, _) = els.kind;
|
2021-03-06 17:08:46 +00:00
|
|
|
if els_block.stmts.is_empty();
|
2021-04-02 21:35:32 +00:00
|
|
|
if let Some(els_expr) = els_block.expr;
|
2021-04-02 22:09:58 +00:00
|
|
|
if let ExprKind::Path(ref qpath) = els_expr.kind;
|
|
|
|
if is_lang_ctor(cx, qpath, OptionNone);
|
2021-03-06 17:08:46 +00:00
|
|
|
then {
|
2021-03-14 23:17:44 +00:00
|
|
|
let cond_snip = snippet_with_macro_callsite(cx, cond.span, "[condition]");
|
2021-03-12 11:46:46 +00:00
|
|
|
let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) {
|
|
|
|
format!("({})", cond_snip)
|
|
|
|
} else {
|
|
|
|
cond_snip.into_owned()
|
|
|
|
};
|
2021-03-14 23:17:44 +00:00
|
|
|
let arg_snip = snippet_with_macro_callsite(cx, then_arg.span, "");
|
2021-03-12 11:46:46 +00:00
|
|
|
let closure_body = if then_block.stmts.is_empty() {
|
|
|
|
arg_snip.into_owned()
|
|
|
|
} else {
|
|
|
|
format!("{{ /* snippet */ {} }}", arg_snip)
|
|
|
|
};
|
2021-03-08 14:19:57 +00:00
|
|
|
let help = format!(
|
2021-03-12 11:46:46 +00:00
|
|
|
"consider using `bool::then` like: `{}.then(|| {})`",
|
2021-03-06 17:08:46 +00:00
|
|
|
cond_snip,
|
2021-03-12 11:46:46 +00:00
|
|
|
closure_body,
|
2021-03-06 17:08:46 +00:00
|
|
|
);
|
2021-03-16 00:55:45 +00:00
|
|
|
span_lint_and_help(
|
2021-03-06 17:08:46 +00:00
|
|
|
cx,
|
|
|
|
IF_THEN_SOME_ELSE_NONE,
|
|
|
|
expr.span,
|
|
|
|
"this could be simplified with `bool::then`",
|
2021-03-08 14:19:57 +00:00
|
|
|
None,
|
|
|
|
&help,
|
2021-03-06 17:08:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 13:52:03 +00:00
|
|
|
|
|
|
|
extract_msrv_attr!(LateContext);
|
2021-03-06 17:08:46 +00:00
|
|
|
}
|