From 0327c2e04106e258949dbf96e58bb582e2960c6b Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Mon, 8 Mar 2021 23:19:57 +0900 Subject: [PATCH] Output help instead of suggestion in `if_then_some_else_none` diagnose --- clippy_lints/src/if_then_some_else_none.rs | 17 +++++++---------- tests/ui/if_then_some_else_none.rs | 4 ++-- tests/ui/if_then_some_else_none.stderr | 9 ++++++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/if_then_some_else_none.rs b/clippy_lints/src/if_then_some_else_none.rs index aadadd0d934..569a7f06f95 100644 --- a/clippy_lints/src/if_then_some_else_none.rs +++ b/clippy_lints/src/if_then_some_else_none.rs @@ -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, ); } } diff --git a/tests/ui/if_then_some_else_none.rs b/tests/ui/if_then_some_else_none.rs index 337292fd9b3..14a5fe76245 100644 --- a/tests/ui/if_then_some_else_none.rs +++ b/tests/ui/if_then_some_else_none.rs @@ -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 }; diff --git a/tests/ui/if_then_some_else_none.stderr b/tests/ui/if_then_some_else_none.stderr index 19c96f900a3..722c52b1cb4 100644 --- a/tests/ui/if_then_some_else_none.stderr +++ b/tests/ui/if_then_some_else_none.stderr @@ -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