Skip to content

Commit 1a1adad

Browse files
committed
Add MSRV option to unnested_or_patterns lint
1 parent f41d68f commit 1a1adad

File tree

5 files changed

+48
-14
lines changed

5 files changed

+48
-14
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10791079
store.register_late_pass(move || box missing_const_for_fn::MissingConstForFn::new(msrv));
10801080
store.register_late_pass(move || box needless_question_mark::NeedlessQuestionMark::new(msrv));
10811081
store.register_late_pass(move || box casts::Casts::new(msrv));
1082+
store.register_early_pass(move || box unnested_or_patterns::UnnestedOrPatterns::new(msrv));
10821083

10831084
store.register_late_pass(|| box size_of_in_element_count::SizeOfInElementCount);
10841085
store.register_late_pass(|| box map_clone::MapClone);
@@ -1254,7 +1255,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12541255
store.register_early_pass(move || box non_expressive_names::NonExpressiveNames {
12551256
single_char_binding_names_threshold,
12561257
});
1257-
store.register_early_pass(|| box unnested_or_patterns::UnnestedOrPatterns);
12581258
store.register_late_pass(|| box macro_use::MacroUseImports::default());
12591259
store.register_late_pass(|| box map_identity::MapIdentity);
12601260
store.register_late_pass(|| box pattern_type_mismatch::PatternTypeMismatch);

clippy_lints/src/unnested_or_patterns.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
22

3-
use clippy_utils::ast_utils::{eq_field_pat, eq_id, eq_pat, eq_path};
43
use clippy_utils::diagnostics::span_lint_and_then;
54
use clippy_utils::over;
5+
use clippy_utils::{
6+
ast_utils::{eq_field_pat, eq_id, eq_pat, eq_path},
7+
meets_msrv,
8+
};
69
use rustc_ast::mut_visit::*;
710
use rustc_ast::ptr::P;
811
use rustc_ast::{self as ast, Pat, PatKind, PatKind::*, DUMMY_NODE_ID};
912
use rustc_ast_pretty::pprust;
1013
use rustc_errors::Applicability;
1114
use rustc_lint::{EarlyContext, EarlyLintPass};
12-
use rustc_session::{declare_lint_pass, declare_tool_lint};
15+
use rustc_semver::RustcVersion;
16+
use rustc_session::{declare_tool_lint, impl_lint_pass};
1317
use rustc_span::DUMMY_SP;
1418

1519
use std::cell::Cell;
@@ -50,26 +54,50 @@ declare_clippy_lint! {
5054
"unnested or-patterns, e.g., `Foo(Bar) | Foo(Baz) instead of `Foo(Bar | Baz)`"
5155
}
5256

53-
declare_lint_pass!(UnnestedOrPatterns => [UNNESTED_OR_PATTERNS]);
57+
const UNNESTED_OR_PATTERNS_MSRV: RustcVersion = RustcVersion::new(1, 53, 0);
58+
59+
#[derive(Clone, Copy)]
60+
pub struct UnnestedOrPatterns {
61+
msrv: Option<RustcVersion>,
62+
}
63+
64+
impl UnnestedOrPatterns {
65+
#[must_use]
66+
pub fn new(msrv: Option<RustcVersion>) -> Self {
67+
Self { msrv }
68+
}
69+
}
70+
71+
impl_lint_pass!(UnnestedOrPatterns => [UNNESTED_OR_PATTERNS]);
5472

5573
impl EarlyLintPass for UnnestedOrPatterns {
5674
fn check_arm(&mut self, cx: &EarlyContext<'_>, a: &ast::Arm) {
57-
lint_unnested_or_patterns(cx, &a.pat);
75+
if meets_msrv(self.msrv.as_ref(), &UNNESTED_OR_PATTERNS_MSRV) {
76+
lint_unnested_or_patterns(cx, &a.pat);
77+
}
5878
}
5979

6080
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
61-
if let ast::ExprKind::Let(pat, _) = &e.kind {
62-
lint_unnested_or_patterns(cx, pat);
81+
if meets_msrv(self.msrv.as_ref(), &UNNESTED_OR_PATTERNS_MSRV) {
82+
if let ast::ExprKind::Let(pat, _) = &e.kind {
83+
lint_unnested_or_patterns(cx, pat);
84+
}
6385
}
6486
}
6587

6688
fn check_param(&mut self, cx: &EarlyContext<'_>, p: &ast::Param) {
67-
lint_unnested_or_patterns(cx, &p.pat);
89+
if meets_msrv(self.msrv.as_ref(), &UNNESTED_OR_PATTERNS_MSRV) {
90+
lint_unnested_or_patterns(cx, &p.pat);
91+
}
6892
}
6993

7094
fn check_local(&mut self, cx: &EarlyContext<'_>, l: &ast::Local) {
71-
lint_unnested_or_patterns(cx, &l.pat);
95+
if meets_msrv(self.msrv.as_ref(), &UNNESTED_OR_PATTERNS_MSRV) {
96+
lint_unnested_or_patterns(cx, &l.pat);
97+
}
7298
}
99+
100+
extract_msrv_attr!(EarlyContext);
73101
}
74102

75103
fn lint_unnested_or_patterns(cx: &EarlyContext<'_>, pat: &Pat) {

clippy_lints/src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ macro_rules! define_Conf {
106106

107107
pub use self::helpers::Conf;
108108
define_Conf! {
109-
/// Lint: REDUNDANT_FIELD_NAMES, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN. The minimum rust version that the project supports
109+
/// Lint: REDUNDANT_FIELD_NAMES, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS. The minimum rust version that the project supports
110110
(msrv, "msrv": Option<String>, None),
111111
/// Lint: BLACKLISTED_NAME. The list of blacklisted names to lint about. NB: `bar` is not here since it has legitimate uses
112112
(blacklisted_names, "blacklisted_names": Vec<String>, ["foo", "baz", "quux"].iter().map(ToString::to_string).collect()),

tests/ui/min_rust_version_attr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ fn missing_const_for_fn() -> i32 {
123123
1
124124
}
125125

126+
fn unnest_or_patterns() {
127+
struct TS(u8, u8);
128+
if let TS(0, x) | TS(1, x) = TS(0, 0) {}
129+
}
130+
126131
fn main() {
127132
filter_map_next();
128133
checked_conversion();
@@ -138,6 +143,7 @@ fn main() {
138143
replace_with_default();
139144
map_unwrap_or();
140145
missing_const_for_fn();
146+
unnest_or_patterns();
141147
}
142148

143149
mod meets_msrv {

tests/ui/min_rust_version_attr.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error: stripping a prefix manually
2-
--> $DIR/min_rust_version_attr.rs:150:24
2+
--> $DIR/min_rust_version_attr.rs:156:24
33
|
44
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
55
| ^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::manual-strip` implied by `-D warnings`
88
note: the prefix was tested here
9-
--> $DIR/min_rust_version_attr.rs:149:9
9+
--> $DIR/min_rust_version_attr.rs:155:9
1010
|
1111
LL | if s.starts_with("hello, ") {
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -17,13 +17,13 @@ LL | assert_eq!(<stripped>.to_uppercase(), "WORLD!");
1717
|
1818

1919
error: stripping a prefix manually
20-
--> $DIR/min_rust_version_attr.rs:162:24
20+
--> $DIR/min_rust_version_attr.rs:168:24
2121
|
2222
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
2323
| ^^^^^^^^^^^^^^^^^^^^
2424
|
2525
note: the prefix was tested here
26-
--> $DIR/min_rust_version_attr.rs:161:9
26+
--> $DIR/min_rust_version_attr.rs:167:9
2727
|
2828
LL | if s.starts_with("hello, ") {
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)