Skip to content

Commit 0327c2e

Browse files
committed
Output help instead of suggestion in if_then_some_else_none diagnose
1 parent f2a85cb commit 0327c2e

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

clippy_lints/src/if_then_some_else_none.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::utils;
22
use if_chain::if_chain;
3-
use rustc_errors::Applicability;
43
use rustc_hir::{Expr, ExprKind};
54
use rustc_lint::{LateContext, LateLintPass, LintContext};
65
use rustc_middle::lint::in_external_macro;
@@ -83,22 +82,20 @@ impl LateLintPass<'_> for IfThenSomeElseNone {
8382
if let ExprKind::Path(ref els_call_qpath) = els_expr.kind;
8483
if utils::match_qpath(els_call_qpath, &utils::paths::OPTION_NONE);
8584
then {
86-
let mut applicability = Applicability::MachineApplicable;
87-
let cond_snip = utils::snippet_with_applicability(cx, cond.span, "[condition]", &mut applicability);
88-
let arg_snip = utils::snippet_with_applicability(cx, then_arg.span, "", &mut applicability);
89-
let sugg = format!(
90-
"{}.then(|| {{ /* snippet */ {} }})",
85+
let cond_snip = utils::snippet(cx, cond.span, "[condition]");
86+
let arg_snip = utils::snippet(cx, then_arg.span, "");
87+
let help = format!(
88+
"consider using `bool::then` like: `{}.then(|| {{ /* snippet */ {} }})`",
9189
cond_snip,
9290
arg_snip,
9391
);
94-
utils::span_lint_and_sugg(
92+
utils::span_lint_and_help(
9593
cx,
9694
IF_THEN_SOME_ELSE_NONE,
9795
expr.span,
9896
"this could be simplified with `bool::then`",
99-
"try this",
100-
sugg,
101-
applicability,
97+
None,
98+
&help,
10299
);
103100
}
104101
}

tests/ui/if_then_some_else_none.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn _msrv_1_49() {
5555
// `bool::then` was stabilized in 1.50. Do not lint this
5656
let _ = if foo() {
5757
println!("true!");
58-
Some("foo")
58+
Some(149)
5959
} else {
6060
None
6161
};
@@ -65,7 +65,7 @@ fn _msrv_1_50() {
6565
#![clippy::msrv = "1.50"]
6666
let _ = if foo() {
6767
println!("true!");
68-
Some("foo")
68+
Some(150)
6969
} else {
7070
None
7171
};

tests/ui/if_then_some_else_none.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,24 @@ LL | | Some("foo")
88
LL | | } else {
99
LL | | None
1010
LL | | };
11-
| |_____^ help: try this: `foo().then(|| { /* snippet */ "foo" })`
11+
| |_____^
1212
|
1313
= note: `-D clippy::if-then-some-else-none` implied by `-D warnings`
14+
= help: consider using `bool::then` like: `foo().then(|| { /* snippet */ "foo" })`
1415

1516
error: this could be simplified with `bool::then`
1617
--> $DIR/if_then_some_else_none.rs:66:13
1718
|
1819
LL | let _ = if foo() {
1920
| _____________^
2021
LL | | println!("true!");
21-
LL | | Some("foo")
22+
LL | | Some(150)
2223
LL | | } else {
2324
LL | | None
2425
LL | | };
25-
| |_____^ help: try this: `foo().then(|| { /* snippet */ "foo" })`
26+
| |_____^
27+
|
28+
= help: consider using `bool::then` like: `foo().then(|| { /* snippet */ 150 })`
2629

2730
error: aborting due to 2 previous errors
2831

0 commit comments

Comments
 (0)