about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYusuke Tanaka <yusuktan@maguro.dev>2021-03-08 22:52:03 +0900
committerYusuke Tanaka <yusuktan@maguro.dev>2021-03-08 22:52:03 +0900
commitf2a85cb42ae64bc5a82eaee49d92b6f9d93153fe (patch)
tree38e947f002db38f644aba5c45a6d76a48a32a83a
parenta672d335a248034369cd41d4e08e02cf378c0e4b (diff)
downloadrust-f2a85cb42ae64bc5a82eaee49d92b6f9d93153fe.tar.gz
rust-f2a85cb42ae64bc5a82eaee49d92b6f9d93153fe.zip
Set 1.50 as msrv of if_then_some_else_none
-rw-r--r--clippy_lints/src/if_then_some_else_none.rs24
-rw-r--r--clippy_lints/src/lib.rs2
-rw-r--r--tests/ui/if_then_some_else_none.rs22
-rw-r--r--tests/ui/if_then_some_else_none.stderr16
4 files changed, 59 insertions, 5 deletions
diff --git a/clippy_lints/src/if_then_some_else_none.rs b/clippy_lints/src/if_then_some_else_none.rs
index 0bd393f8996..aadadd0d934 100644
--- a/clippy_lints/src/if_then_some_else_none.rs
+++ b/clippy_lints/src/if_then_some_else_none.rs
@@ -4,7 +4,10 @@ use rustc_errors::Applicability;
 use rustc_hir::{Expr, ExprKind};
 use rustc_lint::{LateContext, LateLintPass, LintContext};
 use rustc_middle::lint::in_external_macro;
-use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_semver::RustcVersion;
+use rustc_session::{declare_tool_lint, impl_lint_pass};
+
+const IF_THEN_SOME_ELSE_NONE_MSRV: RustcVersion = RustcVersion::new(1, 50, 0);
 
 declare_clippy_lint! {
     /// **What it does:** Checks for if-else that could be written to `bool::then`.
@@ -39,10 +42,25 @@ declare_clippy_lint! {
     "Finds if-else that could be written using `bool::then`"
 }
 
-declare_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]);
+pub struct IfThenSomeElseNone {
+    msrv: Option<RustcVersion>,
+}
+
+impl IfThenSomeElseNone {
+    #[must_use]
+    pub fn new(msrv: Option<RustcVersion>) -> Self {
+        Self { msrv }
+    }
+}
+
+impl_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]);
 
 impl LateLintPass<'_> for IfThenSomeElseNone {
     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
+        if !utils::meets_msrv(self.msrv.as_ref(), &IF_THEN_SOME_ELSE_NONE_MSRV) {
+            return;
+        }
+
         if in_external_macro(cx.sess(), expr.span) {
             return;
         }
@@ -85,4 +103,6 @@ impl LateLintPass<'_> for IfThenSomeElseNone {
             }
         }
     }
+
+    extract_msrv_attr!(LateContext);
 }
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index 9a7dedf416e..2e4c1e3bdf8 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -1284,7 +1284,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
     store.register_late_pass(|| box redundant_slicing::RedundantSlicing);
     store.register_late_pass(|| box from_str_radix_10::FromStrRadix10);
     store.register_late_pass(|| box manual_map::ManualMap);
-    store.register_late_pass(|| box if_then_some_else_none::IfThenSomeElseNone);
+    store.register_late_pass(move || box if_then_some_else_none::IfThenSomeElseNone::new(msrv));
 
     store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
         LintId::of(&arithmetic::FLOAT_ARITHMETIC),
diff --git a/tests/ui/if_then_some_else_none.rs b/tests/ui/if_then_some_else_none.rs
index b19e2a50010..337292fd9b3 100644
--- a/tests/ui/if_then_some_else_none.rs
+++ b/tests/ui/if_then_some_else_none.rs
@@ -1,4 +1,5 @@
 #![warn(clippy::if_then_some_else_none)]
+#![feature(custom_inner_attributes)]
 
 fn main() {
     // Should issue an error.
@@ -49,6 +50,27 @@ fn main() {
     let _ = if foo() { into_some("foo") } else { None };
 }
 
+fn _msrv_1_49() {
+    #![clippy::msrv = "1.49"]
+    // `bool::then` was stabilized in 1.50. Do not lint this
+    let _ = if foo() {
+        println!("true!");
+        Some("foo")
+    } else {
+        None
+    };
+}
+
+fn _msrv_1_50() {
+    #![clippy::msrv = "1.50"]
+    let _ = if foo() {
+        println!("true!");
+        Some("foo")
+    } else {
+        None
+    };
+}
+
 fn foo() -> bool {
     unimplemented!()
 }
diff --git a/tests/ui/if_then_some_else_none.stderr b/tests/ui/if_then_some_else_none.stderr
index 7ad9c4ce79d..19c96f900a3 100644
--- a/tests/ui/if_then_some_else_none.stderr
+++ b/tests/ui/if_then_some_else_none.stderr
@@ -1,5 +1,5 @@
 error: this could be simplified with `bool::then`
-  --> $DIR/if_then_some_else_none.rs:5:13
+  --> $DIR/if_then_some_else_none.rs:6:13
    |
 LL |       let _ = if foo() {
    |  _____________^
@@ -12,5 +12,17 @@ LL | |     };
    |
    = note: `-D clippy::if-then-some-else-none` implied by `-D warnings`
 
-error: aborting due to previous error
+error: this could be simplified with `bool::then`
+  --> $DIR/if_then_some_else_none.rs:66:13
+   |
+LL |       let _ = if foo() {
+   |  _____________^
+LL | |         println!("true!");
+LL | |         Some("foo")
+LL | |     } else {
+LL | |         None
+LL | |     };
+   | |_____^ help: try this: `foo().then(|| { /* snippet */ "foo" })`
+
+error: aborting due to 2 previous errors