Extend if_then_some_else_none
to also suggest bool::then_some
This commit is contained in:
parent
71777465cc
commit
80f0f280df
@ -1,7 +1,7 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_help;
|
use clippy_utils::diagnostics::span_lint_and_help;
|
||||||
|
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
|
||||||
use clippy_utils::source::snippet_with_macro_callsite;
|
use clippy_utils::source::snippet_with_macro_callsite;
|
||||||
use clippy_utils::{contains_return, higher, is_else_clause, is_lang_ctor, meets_msrv, msrvs, peel_blocks};
|
use clippy_utils::{contains_return, higher, is_else_clause, is_lang_ctor, meets_msrv, msrvs, peel_blocks};
|
||||||
use if_chain::if_chain;
|
|
||||||
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
||||||
use rustc_hir::{Expr, ExprKind, Stmt, StmtKind};
|
use rustc_hir::{Expr, ExprKind, Stmt, StmtKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||||
@ -56,7 +56,7 @@ pub fn new(msrv: Option<RustcVersion>) -> Self {
|
|||||||
impl_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]);
|
impl_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]);
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
|
impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
||||||
if !meets_msrv(self.msrv, msrvs::BOOL_THEN) {
|
if !meets_msrv(self.msrv, msrvs::BOOL_THEN) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -70,17 +70,16 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if_chain! {
|
if let Some(higher::If { cond, then, r#else: Some(els) }) = higher::If::hir(expr)
|
||||||
if let Some(higher::If { cond, then, r#else: Some(els) }) = higher::If::hir(expr);
|
&& let ExprKind::Block(then_block, _) = then.kind
|
||||||
if let ExprKind::Block(then_block, _) = then.kind;
|
&& let Some(then_expr) = then_block.expr
|
||||||
if let Some(then_expr) = then_block.expr;
|
&& let ExprKind::Call(then_call, [then_arg]) = then_expr.kind
|
||||||
if let ExprKind::Call(then_call, [then_arg]) = then_expr.kind;
|
&& let ExprKind::Path(ref then_call_qpath) = then_call.kind
|
||||||
if let ExprKind::Path(ref then_call_qpath) = then_call.kind;
|
&& is_lang_ctor(cx, then_call_qpath, OptionSome)
|
||||||
if is_lang_ctor(cx, then_call_qpath, OptionSome);
|
&& let ExprKind::Path(ref qpath) = peel_blocks(els).kind
|
||||||
if let ExprKind::Path(ref qpath) = peel_blocks(els).kind;
|
&& is_lang_ctor(cx, qpath, OptionNone)
|
||||||
if is_lang_ctor(cx, qpath, OptionNone);
|
&& !stmts_contains_early_return(then_block.stmts)
|
||||||
if !stmts_contains_early_return(then_block.stmts);
|
{
|
||||||
then {
|
|
||||||
let cond_snip = snippet_with_macro_callsite(cx, cond.span, "[condition]");
|
let cond_snip = snippet_with_macro_callsite(cx, cond.span, "[condition]");
|
||||||
let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) {
|
let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) {
|
||||||
format!("({})", cond_snip)
|
format!("({})", cond_snip)
|
||||||
@ -88,27 +87,32 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
|
|||||||
cond_snip.into_owned()
|
cond_snip.into_owned()
|
||||||
};
|
};
|
||||||
let arg_snip = snippet_with_macro_callsite(cx, then_arg.span, "");
|
let arg_snip = snippet_with_macro_callsite(cx, then_arg.span, "");
|
||||||
let closure_body = if then_block.stmts.is_empty() {
|
let mut method_body = if then_block.stmts.is_empty() {
|
||||||
arg_snip.into_owned()
|
arg_snip.into_owned()
|
||||||
} else {
|
} else {
|
||||||
format!("{{ /* snippet */ {} }}", arg_snip)
|
format!("{{ /* snippet */ {} }}", arg_snip)
|
||||||
};
|
};
|
||||||
|
let method_name = if switch_to_eager_eval(cx, expr) && meets_msrv(self.msrv, msrvs::BOOL_THEN_SOME) {
|
||||||
|
"then_some"
|
||||||
|
} else {
|
||||||
|
method_body.insert_str(0, "|| ");
|
||||||
|
"then"
|
||||||
|
};
|
||||||
|
|
||||||
let help = format!(
|
let help = format!(
|
||||||
"consider using `bool::then` like: `{}.then(|| {})`",
|
"consider using `bool::{}` like: `{}.{}({})`",
|
||||||
cond_snip,
|
method_name, cond_snip, method_name, method_body,
|
||||||
closure_body,
|
|
||||||
);
|
);
|
||||||
span_lint_and_help(
|
span_lint_and_help(
|
||||||
cx,
|
cx,
|
||||||
IF_THEN_SOME_ELSE_NONE,
|
IF_THEN_SOME_ELSE_NONE,
|
||||||
expr.span,
|
expr.span,
|
||||||
"this could be simplified with `bool::then`",
|
&format!("this could be simplified with `bool::{}`", method_name),
|
||||||
None,
|
None,
|
||||||
&help,
|
&help,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
extract_msrv_attr!(LateContext);
|
extract_msrv_attr!(LateContext);
|
||||||
}
|
}
|
||||||
|
@ -27,21 +27,21 @@ LL | | };
|
|||||||
|
|
|
|
||||||
= help: consider using `bool::then` like: `matches!(true, true).then(|| { /* snippet */ matches!(true, false) })`
|
= help: consider using `bool::then` like: `matches!(true, true).then(|| { /* snippet */ matches!(true, false) })`
|
||||||
|
|
||||||
error: this could be simplified with `bool::then`
|
error: this could be simplified with `bool::then_some`
|
||||||
--> $DIR/if_then_some_else_none.rs:23:28
|
--> $DIR/if_then_some_else_none.rs:23:28
|
||||||
|
|
|
|
||||||
LL | let _ = x.and_then(|o| if o < 32 { Some(o) } else { None });
|
LL | let _ = x.and_then(|o| if o < 32 { Some(o) } else { None });
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
= help: consider using `bool::then` like: `(o < 32).then(|| o)`
|
= help: consider using `bool::then_some` like: `(o < 32).then_some(o)`
|
||||||
|
|
||||||
error: this could be simplified with `bool::then`
|
error: this could be simplified with `bool::then_some`
|
||||||
--> $DIR/if_then_some_else_none.rs:27:13
|
--> $DIR/if_then_some_else_none.rs:27:13
|
||||||
|
|
|
|
||||||
LL | let _ = if !x { Some(0) } else { None };
|
LL | let _ = if !x { Some(0) } else { None };
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
= help: consider using `bool::then` like: `(!x).then(|| 0)`
|
= help: consider using `bool::then_some` like: `(!x).then_some(0)`
|
||||||
|
|
||||||
error: this could be simplified with `bool::then`
|
error: this could be simplified with `bool::then`
|
||||||
--> $DIR/if_then_some_else_none.rs:82:13
|
--> $DIR/if_then_some_else_none.rs:82:13
|
||||||
|
Loading…
Reference in New Issue
Block a user