Output help instead of suggestion in if_then_some_else_none diagnose

This commit is contained in:
Yusuke Tanaka 2021-03-08 23:19:57 +09:00
parent f2a85cb42a
commit 0327c2e041
No known key found for this signature in database
GPG key ID: 409D7EEE1E7A716A
3 changed files with 15 additions and 15 deletions

View file

@ -1,6 +1,5 @@
use crate::utils;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
@ -83,22 +82,20 @@ impl LateLintPass<'_> for IfThenSomeElseNone {
if let ExprKind::Path(ref els_call_qpath) = els_expr.kind;
if utils::match_qpath(els_call_qpath, &utils::paths::OPTION_NONE);
then {
let mut applicability = Applicability::MachineApplicable;
let cond_snip = utils::snippet_with_applicability(cx, cond.span, "[condition]", &mut applicability);
let arg_snip = utils::snippet_with_applicability(cx, then_arg.span, "", &mut applicability);
let sugg = format!(
"{}.then(|| {{ /* snippet */ {} }})",
let cond_snip = utils::snippet(cx, cond.span, "[condition]");
let arg_snip = utils::snippet(cx, then_arg.span, "");
let help = format!(
"consider using `bool::then` like: `{}.then(|| {{ /* snippet */ {} }})`",
cond_snip,
arg_snip,
);
utils::span_lint_and_sugg(
utils::span_lint_and_help(
cx,
IF_THEN_SOME_ELSE_NONE,
expr.span,
"this could be simplified with `bool::then`",
"try this",
sugg,
applicability,
None,
&help,
);
}
}

View file

@ -55,7 +55,7 @@ fn _msrv_1_49() {
// `bool::then` was stabilized in 1.50. Do not lint this
let _ = if foo() {
println!("true!");
Some("foo")
Some(149)
} else {
None
};
@ -65,7 +65,7 @@ fn _msrv_1_50() {
#![clippy::msrv = "1.50"]
let _ = if foo() {
println!("true!");
Some("foo")
Some(150)
} else {
None
};

View file

@ -8,9 +8,10 @@ LL | | Some("foo")
LL | | } else {
LL | | None
LL | | };
| |_____^ help: try this: `foo().then(|| { /* snippet */ "foo" })`
| |_____^
|
= note: `-D clippy::if-then-some-else-none` implied by `-D warnings`
= help: consider using `bool::then` like: `foo().then(|| { /* snippet */ "foo" })`
error: this could be simplified with `bool::then`
--> $DIR/if_then_some_else_none.rs:66:13
@ -18,11 +19,13 @@ error: this could be simplified with `bool::then`
LL | let _ = if foo() {
| _____________^
LL | | println!("true!");
LL | | Some("foo")
LL | | Some(150)
LL | | } else {
LL | | None
LL | | };
| |_____^ help: try this: `foo().then(|| { /* snippet */ "foo" })`
| |_____^
|
= help: consider using `bool::then` like: `foo().then(|| { /* snippet */ 150 })`
error: aborting due to 2 previous errors