Skip to content

Commit 413d255

Browse files
committed
Add MSRV to deprecated_cfg_attr
1 parent 07f4f7c commit 413d255

File tree

7 files changed

+42
-19
lines changed

7 files changed

+42
-19
lines changed

clippy_lints/src/attrs.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! checks for attributes
22
33
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
4-
use clippy_utils::match_panic_def_id;
4+
use clippy_utils::msrvs;
55
use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments};
6+
use clippy_utils::{extract_msrv_attr, match_panic_def_id, meets_msrv};
67
use if_chain::if_chain;
78
use rustc_ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
89
use rustc_errors::Applicability;
@@ -12,7 +13,8 @@ use rustc_hir::{
1213
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
1314
use rustc_middle::lint::in_external_macro;
1415
use rustc_middle::ty;
15-
use rustc_session::{declare_lint_pass, declare_tool_lint};
16+
use rustc_semver::RustcVersion;
17+
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
1618
use rustc_span::source_map::Span;
1719
use rustc_span::sym;
1820
use rustc_span::symbol::{Symbol, SymbolStr};
@@ -497,7 +499,11 @@ fn is_word(nmi: &NestedMetaItem, expected: Symbol) -> bool {
497499
}
498500
}
499501

500-
declare_lint_pass!(EarlyAttributes => [
502+
pub struct EarlyAttributes {
503+
pub msrv: Option<RustcVersion>,
504+
}
505+
506+
impl_lint_pass!(EarlyAttributes => [
501507
DEPRECATED_CFG_ATTR,
502508
MISMATCHED_TARGET_OS,
503509
EMPTY_LINE_AFTER_OUTER_ATTR,
@@ -509,9 +515,11 @@ impl EarlyLintPass for EarlyAttributes {
509515
}
510516

511517
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
512-
check_deprecated_cfg_attr(cx, attr);
518+
check_deprecated_cfg_attr(cx, attr, self.msrv);
513519
check_mismatched_target_os(cx, attr);
514520
}
521+
522+
extract_msrv_attr!(EarlyContext);
515523
}
516524

517525
fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::Item) {
@@ -548,8 +556,9 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It
548556
}
549557
}
550558

551-
fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute) {
559+
fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: Option<RustcVersion>) {
552560
if_chain! {
561+
if meets_msrv(msrv.as_ref(), &msrvs::TOOL_ATTRIBUTES);
553562
// check cfg_attr
554563
if attr.has_name(sym::cfg_attr);
555564
if let Some(items) = attr.meta_item_list();

clippy_lints/src/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,21 @@ use crate::utils::conf::TryConf;
404404
/// level (i.e `#![cfg_attr(...)]`) will still be expanded even when using a pre-expansion pass.
405405
///
406406
/// Used in `./src/driver.rs`.
407-
pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore) {
407+
pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore, sess: &Session, conf: &Conf) {
408408
// NOTE: Do not add any more pre-expansion passes. These should be removed eventually.
409+
410+
let msrv = conf.msrv.as_ref().and_then(|s| {
411+
parse_msrv(s, None, None).or_else(|| {
412+
sess.err(&format!(
413+
"error reading Clippy's configuration file. `{}` is not a valid Rust version",
414+
s
415+
));
416+
None
417+
})
418+
});
419+
409420
store.register_pre_expansion_pass(|| Box::new(write::Write::default()));
410-
store.register_pre_expansion_pass(|| Box::new(attrs::EarlyAttributes));
421+
store.register_pre_expansion_pass(move || Box::new(attrs::EarlyAttributes { msrv }));
411422
store.register_pre_expansion_pass(|| Box::new(dbg_macro::DbgMacro));
412423
}
413424

clippy_lints/src/utils/conf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ define_Conf! {
148148
///
149149
/// Suppress lints whenever the suggested change would cause breakage for other crates.
150150
(avoid_breaking_exported_api: bool = true),
151-
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, 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, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT.
151+
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, 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, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT, DEPRECATED_CFG_ATTR.
152152
///
153153
/// The minimum rust version that the project supports
154154
(msrv: Option<String> = None),

clippy_utils/src/msrvs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ msrv_aliases! {
2727
1,36,0 { ITERATOR_COPIED }
2828
1,35,0 { OPTION_COPIED, RANGE_CONTAINS }
2929
1,34,0 { TRY_FROM }
30-
1,30,0 { ITERATOR_FIND_MAP }
30+
1,30,0 { ITERATOR_FIND_MAP, TOOL_ATTRIBUTES }
3131
1,17,0 { FIELD_INIT_SHORTHAND, STATIC_IN_CONST }
3232
1,16,0 { STR_REPEAT }
3333
}

src/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
108108

109109
let conf = clippy_lints::read_conf(sess);
110110
clippy_lints::register_plugins(lint_store, sess, &conf);
111-
clippy_lints::register_pre_expansion_lints(lint_store);
111+
clippy_lints::register_pre_expansion_lints(lint_store, sess, &conf);
112112
clippy_lints::register_renamed(lint_store);
113113
}));
114114

tests/ui/min_rust_version_attr.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ fn unnest_or_patterns() {
137137
if let TS(0, x) | TS(1, x) = TS(0, 0) {}
138138
}
139139

140+
#[cfg_attr(rustfmt, rustfmt_skip)]
141+
fn deprecated_cfg_attr() {}
142+
140143
fn main() {
141144
filter_map_next();
142145
checked_conversion();
@@ -155,9 +158,9 @@ fn main() {
155158
unnest_or_patterns();
156159
}
157160

158-
mod meets_msrv {
161+
mod just_under_msrv {
159162
#![feature(custom_inner_attributes)]
160-
#![clippy::msrv = "1.45.0"]
163+
#![clippy::msrv = "1.44.0"]
161164

162165
fn main() {
163166
let s = "hello, world!";
@@ -167,9 +170,9 @@ mod meets_msrv {
167170
}
168171
}
169172

170-
mod just_under_msrv {
173+
mod meets_msrv {
171174
#![feature(custom_inner_attributes)]
172-
#![clippy::msrv = "1.46.0"]
175+
#![clippy::msrv = "1.45.0"]
173176

174177
fn main() {
175178
let s = "hello, world!";
@@ -181,7 +184,7 @@ mod just_under_msrv {
181184

182185
mod just_above_msrv {
183186
#![feature(custom_inner_attributes)]
184-
#![clippy::msrv = "1.44.0"]
187+
#![clippy::msrv = "1.46.0"]
185188

186189
fn main() {
187190
let s = "hello, world!";

tests/ui/min_rust_version_attr.stderr

+4-4
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:165:24
2+
--> $DIR/min_rust_version_attr.rs:180: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:164:9
9+
--> $DIR/min_rust_version_attr.rs:179: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:177:24
20+
--> $DIR/min_rust_version_attr.rs:192: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:176:9
26+
--> $DIR/min_rust_version_attr.rs:191:9
2727
|
2828
LL | if s.starts_with("hello, ") {
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)