From f2a85cb42ae64bc5a82eaee49d92b6f9d93153fe Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Mon, 8 Mar 2021 22:52:03 +0900 Subject: [PATCH] Set 1.50 as msrv of if_then_some_else_none --- clippy_lints/src/if_then_some_else_none.rs | 24 ++++++++++++++++++++-- clippy_lints/src/lib.rs | 2 +- tests/ui/if_then_some_else_none.rs | 22 ++++++++++++++++++++ tests/ui/if_then_some_else_none.stderr | 16 +++++++++++++-- 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 0bd393f89968..aadadd0d934a 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, +} + +impl IfThenSomeElseNone { + #[must_use] + pub fn new(msrv: Option) -> 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 9a7dedf416e4..2e4c1e3bdf8e 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 b19e2a50010f..337292fd9b3d 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 7ad9c4ce79df..19c96f900a30 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