Skip to content

Commit 6433d79

Browse files
committed
Restrict suggestion of deriving Default for enums to MSRV 1.62.
See https://blog.rust-lang.org/2022/06/30/Rust-1.62.0.html#default-enum-variants
1 parent 61ff54e commit 6433d79

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

clippy_lints/src/derivable_impls.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::msrvs::{self, Msrv};
23
use clippy_utils::source::indent_of;
34
use clippy_utils::{is_default_equivalent, peel_blocks};
45
use rustc_errors::Applicability;
@@ -8,7 +9,7 @@ use rustc_hir::{
89
};
910
use rustc_lint::{LateContext, LateLintPass};
1011
use rustc_middle::ty::{AdtDef, DefIdTree};
11-
use rustc_session::{declare_lint_pass, declare_tool_lint};
12+
use rustc_session::{declare_tool_lint, impl_lint_pass};
1213
use rustc_span::sym;
1314

1415
declare_clippy_lint! {
@@ -53,7 +54,18 @@ declare_clippy_lint! {
5354
"manual implementation of the `Default` trait which is equal to a derive"
5455
}
5556

56-
declare_lint_pass!(DerivableImpls => [DERIVABLE_IMPLS]);
57+
pub struct DerivableImpls {
58+
msrv: Msrv,
59+
}
60+
61+
impl DerivableImpls {
62+
#[must_use]
63+
pub fn new(msrv: Msrv) -> Self {
64+
DerivableImpls { msrv }
65+
}
66+
}
67+
68+
impl_lint_pass!(DerivableImpls => [DERIVABLE_IMPLS]);
5769

5870
fn is_path_self(e: &Expr<'_>) -> bool {
5971
if let ExprKind::Path(QPath::Resolved(_, p)) = e.kind {
@@ -181,10 +193,12 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
181193
then {
182194
if adt_def.is_struct() {
183195
check_struct(cx, item, self_ty, func_expr, adt_def);
184-
} else if adt_def.is_enum() {
196+
} else if adt_def.is_enum() && self.msrv.meets(msrvs::DEFAULT_ENUM_ATTRIBUTE) {
185197
check_enum(cx, item, func_expr, adt_def);
186198
}
187199
}
188200
}
189201
}
202+
203+
extract_msrv_attr!(LateContext);
190204
}

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
639639
store.register_late_pass(|_| Box::new(panic_unimplemented::PanicUnimplemented));
640640
store.register_late_pass(|_| Box::new(strings::StringLitAsBytes));
641641
store.register_late_pass(|_| Box::new(derive::Derive));
642-
store.register_late_pass(|_| Box::new(derivable_impls::DerivableImpls));
642+
store.register_late_pass(move |_| Box::new(derivable_impls::DerivableImpls::new(msrv())));
643643
store.register_late_pass(|_| Box::new(drop_forget_ref::DropForgetRef));
644644
store.register_late_pass(|_| Box::new(empty_enum::EmptyEnum));
645645
store.register_late_pass(|_| Box::new(invalid_upcast_comparisons::InvalidUpcastComparisons));

clippy_utils/src/msrvs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ macro_rules! msrv_aliases {
2020
// names may refer to stabilized feature flags or library items
2121
msrv_aliases! {
2222
1,65,0 { LET_ELSE }
23-
1,62,0 { BOOL_THEN_SOME }
23+
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE }
2424
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY }
25+
1,55,0 { SEEK_REWIND }
2526
1,53,0 { OR_PATTERNS, MANUAL_BITS, BTREE_MAP_RETAIN, BTREE_SET_RETAIN, ARRAY_INTO_ITERATOR }
2627
1,52,0 { STR_SPLIT_ONCE, REM_EUCLID_CONST }
2728
1,51,0 { BORROW_AS_PTR, SEEK_FROM_CURRENT, UNSIGNED_ABS }
@@ -45,7 +46,6 @@ msrv_aliases! {
4546
1,18,0 { HASH_MAP_RETAIN, HASH_SET_RETAIN }
4647
1,17,0 { FIELD_INIT_SHORTHAND, STATIC_IN_CONST, EXPECT_ERR }
4748
1,16,0 { STR_REPEAT }
48-
1,55,0 { SEEK_REWIND }
4949
}
5050

5151
fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option<Span>) -> Option<RustcVersion> {

0 commit comments

Comments
 (0)