Set 1.50 as msrv of if_then_some_else_none

This commit is contained in:
Yusuke Tanaka 2021-03-08 22:52:03 +09:00
parent a672d335a2
commit f2a85cb42a
No known key found for this signature in database
GPG key ID: 409D7EEE1E7A716A
4 changed files with 59 additions and 5 deletions

View file

@ -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);
}

View file

@ -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),

View file

@ -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!()
}

View file

@ -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