]> git.lizzy.rs Git - rust.git/commitdiff
Output help instead of suggestion in `if_then_some_else_none` diagnose
authorYusuke Tanaka <yusuktan@maguro.dev>
Mon, 8 Mar 2021 14:19:57 +0000 (23:19 +0900)
committerYusuke Tanaka <yusuktan@maguro.dev>
Mon, 8 Mar 2021 14:20:18 +0000 (23:20 +0900)
clippy_lints/src/if_then_some_else_none.rs
tests/ui/if_then_some_else_none.rs
tests/ui/if_then_some_else_none.stderr

index aadadd0d934a573d25be4f2c6a79b533f76f5351..569a7f06f95bfb7f46359272c6186398ee2d645f 100644 (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 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
             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,
                 );
             }
         }
index 337292fd9b3dbe77473bb3e867343c49ccdaa736..14a5fe76245aa50940fcaccc00cdeb0d7156f77a 100644 (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
     };
index 19c96f900a30328baeff96a7992b50ffc2736542..722c52b1cb43ae826d78323aa49aef22fe8d5bcd 100644 (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