Skip to content

Commit f2a85cb

Browse files
committed
Set 1.50 as msrv of if_then_some_else_none
1 parent a672d33 commit f2a85cb

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

clippy_lints/src/if_then_some_else_none.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use rustc_errors::Applicability;
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass, LintContext};
66
use rustc_middle::lint::in_external_macro;
7-
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_semver::RustcVersion;
8+
use rustc_session::{declare_tool_lint, impl_lint_pass};
9+
10+
const IF_THEN_SOME_ELSE_NONE_MSRV: RustcVersion = RustcVersion::new(1, 50, 0);
811

912
declare_clippy_lint! {
1013
/// **What it does:** Checks for if-else that could be written to `bool::then`.
@@ -39,10 +42,25 @@ declare_clippy_lint! {
3942
"Finds if-else that could be written using `bool::then`"
4043
}
4144

42-
declare_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]);
45+
pub struct IfThenSomeElseNone {
46+
msrv: Option<RustcVersion>,
47+
}
48+
49+
impl IfThenSomeElseNone {
50+
#[must_use]
51+
pub fn new(msrv: Option<RustcVersion>) -> Self {
52+
Self { msrv }
53+
}
54+
}
55+
56+
impl_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]);
4357

4458
impl LateLintPass<'_> for IfThenSomeElseNone {
4559
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
60+
if !utils::meets_msrv(self.msrv.as_ref(), &IF_THEN_SOME_ELSE_NONE_MSRV) {
61+
return;
62+
}
63+
4664
if in_external_macro(cx.sess(), expr.span) {
4765
return;
4866
}
@@ -85,4 +103,6 @@ impl LateLintPass<'_> for IfThenSomeElseNone {
85103
}
86104
}
87105
}
106+
107+
extract_msrv_attr!(LateContext);
88108
}

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12841284
store.register_late_pass(|| box redundant_slicing::RedundantSlicing);
12851285
store.register_late_pass(|| box from_str_radix_10::FromStrRadix10);
12861286
store.register_late_pass(|| box manual_map::ManualMap);
1287-
store.register_late_pass(|| box if_then_some_else_none::IfThenSomeElseNone);
1287+
store.register_late_pass(move || box if_then_some_else_none::IfThenSomeElseNone::new(msrv));
12881288

12891289
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
12901290
LintId::of(&arithmetic::FLOAT_ARITHMETIC),

tests/ui/if_then_some_else_none.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::if_then_some_else_none)]
2+
#![feature(custom_inner_attributes)]
23

34
fn main() {
45
// Should issue an error.
@@ -49,6 +50,27 @@ fn main() {
4950
let _ = if foo() { into_some("foo") } else { None };
5051
}
5152

53+
fn _msrv_1_49() {
54+
#![clippy::msrv = "1.49"]
55+
// `bool::then` was stabilized in 1.50. Do not lint this
56+
let _ = if foo() {
57+
println!("true!");
58+
Some("foo")
59+
} else {
60+
None
61+
};
62+
}
63+
64+
fn _msrv_1_50() {
65+
#![clippy::msrv = "1.50"]
66+
let _ = if foo() {
67+
println!("true!");
68+
Some("foo")
69+
} else {
70+
None
71+
};
72+
}
73+
5274
fn foo() -> bool {
5375
unimplemented!()
5476
}

tests/ui/if_then_some_else_none.stderr

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this could be simplified with `bool::then`
2-
--> $DIR/if_then_some_else_none.rs:5:13
2+
--> $DIR/if_then_some_else_none.rs:6:13
33
|
44
LL | let _ = if foo() {
55
| _____________^
@@ -12,5 +12,17 @@ LL | | };
1212
|
1313
= note: `-D clippy::if-then-some-else-none` implied by `-D warnings`
1414

15-
error: aborting due to previous error
15+
error: this could be simplified with `bool::then`
16+
--> $DIR/if_then_some_else_none.rs:66:13
17+
|
18+
LL | let _ = if foo() {
19+
| _____________^
20+
LL | | println!("true!");
21+
LL | | Some("foo")
22+
LL | | } else {
23+
LL | | None
24+
LL | | };
25+
| |_____^ help: try this: `foo().then(|| { /* snippet */ "foo" })`
26+
27+
error: aborting due to 2 previous errors
1628

0 commit comments

Comments
 (0)