deprecate maybe_misused_cfg

This commit is contained in:
y21 2024-06-01 13:44:13 +02:00
parent 28e887fe71
commit 4aa20d2e95
12 changed files with 43 additions and 217 deletions

View file

@ -1,51 +0,0 @@
use super::{Attribute, MAYBE_MISUSED_CFG};
use clippy_utils::diagnostics::span_lint_and_sugg;
use rustc_ast::{MetaItemKind, NestedMetaItem};
use rustc_errors::Applicability;
use rustc_lint::EarlyContext;
use rustc_span::sym;
pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute) {
if attr.has_name(sym::cfg)
&& let Some(items) = attr.meta_item_list()
{
check_nested_misused_cfg(cx, &items);
}
}
fn check_nested_misused_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
for item in items {
if let NestedMetaItem::MetaItem(meta) = item {
if let Some(ident) = meta.ident()
&& ident.name.as_str() == "features"
&& let Some(val) = meta.value_str()
{
span_lint_and_sugg(
cx,
MAYBE_MISUSED_CFG,
meta.span,
"'feature' may be misspelled as 'features'",
"did you mean",
format!("feature = \"{val}\""),
Applicability::MaybeIncorrect,
);
}
if let MetaItemKind::List(list) = &meta.kind {
check_nested_misused_cfg(cx, list);
// If this is not a list, then we check for `cfg(test)`.
} else if let Some(ident) = meta.ident()
&& matches!(ident.name.as_str(), "tests" | "Test")
{
span_lint_and_sugg(
cx,
MAYBE_MISUSED_CFG,
meta.span,
format!("'test' may be misspelled as '{}'", ident.name.as_str()),
"did you mean",
"test".to_string(),
Applicability::MaybeIncorrect,
);
}
}
}
}

View file

@ -7,7 +7,6 @@ mod deprecated_semver;
mod duplicated_attributes; mod duplicated_attributes;
mod empty_line_after; mod empty_line_after;
mod inline_always; mod inline_always;
mod maybe_misused_cfg;
mod mismatched_target_os; mod mismatched_target_os;
mod mixed_attributes_style; mod mixed_attributes_style;
mod non_minimal_cfg; mod non_minimal_cfg;
@ -391,38 +390,6 @@ declare_clippy_lint! {
"ensure that all `cfg(any())` and `cfg(all())` have more than one condition" "ensure that all `cfg(any())` and `cfg(all())` have more than one condition"
} }
declare_clippy_lint! {
/// ### What it does
/// Checks for `#[cfg(features = "...")]` and suggests to replace it with
/// `#[cfg(feature = "...")]`.
///
/// It also checks if `cfg(test)` was misspelled.
///
/// ### Why is this bad?
/// Misspelling `feature` as `features` or `test` as `tests` can be sometimes hard to spot. It
/// may cause conditional compilation not work quietly.
///
/// ### Example
/// ```no_run
/// #[cfg(features = "some-feature")]
/// fn conditional() { }
/// #[cfg(tests)]
/// mod tests { }
/// ```
///
/// Use instead:
/// ```no_run
/// #[cfg(feature = "some-feature")]
/// fn conditional() { }
/// #[cfg(test)]
/// mod tests { }
/// ```
#[clippy::version = "1.69.0"]
pub MAYBE_MISUSED_CFG,
suspicious,
"prevent from misusing the wrong attr name"
}
declare_clippy_lint! { declare_clippy_lint! {
/// ### What it does /// ### What it does
/// Checks for `#[cfg_attr(feature = "cargo-clippy", ...)]` and for /// Checks for `#[cfg_attr(feature = "cargo-clippy", ...)]` and for
@ -616,7 +583,6 @@ impl_lint_pass!(EarlyAttributes => [
EMPTY_LINE_AFTER_OUTER_ATTR, EMPTY_LINE_AFTER_OUTER_ATTR,
EMPTY_LINE_AFTER_DOC_COMMENTS, EMPTY_LINE_AFTER_DOC_COMMENTS,
NON_MINIMAL_CFG, NON_MINIMAL_CFG,
MAYBE_MISUSED_CFG,
DEPRECATED_CLIPPY_CFG_ATTR, DEPRECATED_CLIPPY_CFG_ATTR,
UNNECESSARY_CLIPPY_CFG, UNNECESSARY_CLIPPY_CFG,
]); ]);
@ -631,7 +597,6 @@ impl EarlyLintPass for EarlyAttributes {
deprecated_cfg_attr::check_clippy(cx, attr); deprecated_cfg_attr::check_clippy(cx, attr);
mismatched_target_os::check(cx, attr); mismatched_target_os::check(cx, attr);
non_minimal_cfg::check(cx, attr); non_minimal_cfg::check(cx, attr);
maybe_misused_cfg::check(cx, attr);
} }
extract_msrv_attr!(EarlyContext); extract_msrv_attr!(EarlyContext);

View file

@ -58,7 +58,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::attrs::EMPTY_LINE_AFTER_DOC_COMMENTS_INFO, crate::attrs::EMPTY_LINE_AFTER_DOC_COMMENTS_INFO,
crate::attrs::EMPTY_LINE_AFTER_OUTER_ATTR_INFO, crate::attrs::EMPTY_LINE_AFTER_OUTER_ATTR_INFO,
crate::attrs::INLINE_ALWAYS_INFO, crate::attrs::INLINE_ALWAYS_INFO,
crate::attrs::MAYBE_MISUSED_CFG_INFO,
crate::attrs::MISMATCHED_TARGET_OS_INFO, crate::attrs::MISMATCHED_TARGET_OS_INFO,
crate::attrs::MIXED_ATTRIBUTES_STYLE_INFO, crate::attrs::MIXED_ATTRIBUTES_STYLE_INFO,
crate::attrs::NON_MINIMAL_CFG_INFO, crate::attrs::NON_MINIMAL_CFG_INFO,

View file

@ -215,3 +215,16 @@ declare_deprecated_lint! {
pub WRONG_PUB_SELF_CONVENTION, pub WRONG_PUB_SELF_CONVENTION,
"set the `avoid-breaking-exported-api` config option to `false` to enable the `wrong_self_convention` lint for public items" "set the `avoid-breaking-exported-api` config option to `false` to enable the `wrong_self_convention` lint for public items"
} }
declare_deprecated_lint! {
/// ### What it does
/// Nothing. This lint has been deprecated.
///
/// ### Deprecation reason
/// This lint has been superseded by rustc's own [`unexpected_cfgs`] lint that is able to detect the `#[cfg(features)]` and `#[cfg(tests)]` typos.
///
/// [`unexpected_cfgs`]: https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#unexpected-cfgs
#[clippy::version = "1.80.0"]
pub MAYBE_MISUSED_CFG,
"this lint has been replaced by `unexpected_cfgs`"
}

View file

@ -67,4 +67,8 @@
"clippy::wrong_pub_self_convention", "clippy::wrong_pub_self_convention",
"set the `avoid-breaking-exported-api` config option to `false` to enable the `wrong_self_convention` lint for public items", "set the `avoid-breaking-exported-api` config option to `false` to enable the `wrong_self_convention` lint for public items",
); );
store.register_removed(
"clippy::maybe_misused_cfg",
"this lint has been replaced by `unexpected_cfgs`",
);
} }

View file

@ -1,29 +0,0 @@
#![warn(clippy::maybe_misused_cfg)]
fn main() {
#[cfg(feature = "not-really-a-feature")]
//~^ ERROR: 'feature' may be misspelled as 'features'
//~| NOTE: `-D clippy::maybe-misused-cfg` implied by `-D warnings`
let _ = 1 + 2;
#[cfg(all(feature = "right", feature = "wrong"))]
//~^ ERROR: 'feature' may be misspelled as 'features'
let _ = 1 + 2;
#[cfg(all(feature = "wrong1", any(feature = "right", feature = "wrong2", feature, features)))]
//~^ ERROR: 'feature' may be misspelled as 'features'
//~| ERROR: 'feature' may be misspelled as 'features'
let _ = 1 + 2;
#[cfg(test)]
//~^ ERROR: 'test' may be misspelled as 'tests'
let _ = 2;
#[cfg(test)]
//~^ ERROR: 'test' may be misspelled as 'Test'
let _ = 2;
#[cfg(all(test, test))]
//~^ ERROR: 'test' may be misspelled as 'tests'
//~| ERROR: 'test' may be misspelled as 'Test'
let _ = 2;
}

View file

@ -1,29 +0,0 @@
#![warn(clippy::maybe_misused_cfg)]
fn main() {
#[cfg(features = "not-really-a-feature")]
//~^ ERROR: 'feature' may be misspelled as 'features'
//~| NOTE: `-D clippy::maybe-misused-cfg` implied by `-D warnings`
let _ = 1 + 2;
#[cfg(all(feature = "right", features = "wrong"))]
//~^ ERROR: 'feature' may be misspelled as 'features'
let _ = 1 + 2;
#[cfg(all(features = "wrong1", any(feature = "right", features = "wrong2", feature, features)))]
//~^ ERROR: 'feature' may be misspelled as 'features'
//~| ERROR: 'feature' may be misspelled as 'features'
let _ = 1 + 2;
#[cfg(tests)]
//~^ ERROR: 'test' may be misspelled as 'tests'
let _ = 2;
#[cfg(Test)]
//~^ ERROR: 'test' may be misspelled as 'Test'
let _ = 2;
#[cfg(all(tests, Test))]
//~^ ERROR: 'test' may be misspelled as 'tests'
//~| ERROR: 'test' may be misspelled as 'Test'
let _ = 2;
}

View file

@ -1,53 +0,0 @@
error: 'feature' may be misspelled as 'features'
--> tests/ui/cfg_features.rs:4:11
|
LL | #[cfg(features = "not-really-a-feature")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `feature = "not-really-a-feature"`
|
= note: `-D clippy::maybe-misused-cfg` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::maybe_misused_cfg)]`
error: 'feature' may be misspelled as 'features'
--> tests/ui/cfg_features.rs:9:34
|
LL | #[cfg(all(feature = "right", features = "wrong"))]
| ^^^^^^^^^^^^^^^^^^ help: did you mean: `feature = "wrong"`
error: 'feature' may be misspelled as 'features'
--> tests/ui/cfg_features.rs:13:15
|
LL | #[cfg(all(features = "wrong1", any(feature = "right", features = "wrong2", feature, features)))]
| ^^^^^^^^^^^^^^^^^^^ help: did you mean: `feature = "wrong1"`
error: 'feature' may be misspelled as 'features'
--> tests/ui/cfg_features.rs:13:59
|
LL | #[cfg(all(features = "wrong1", any(feature = "right", features = "wrong2", feature, features)))]
| ^^^^^^^^^^^^^^^^^^^ help: did you mean: `feature = "wrong2"`
error: 'test' may be misspelled as 'tests'
--> tests/ui/cfg_features.rs:18:11
|
LL | #[cfg(tests)]
| ^^^^^ help: did you mean: `test`
error: 'test' may be misspelled as 'Test'
--> tests/ui/cfg_features.rs:21:11
|
LL | #[cfg(Test)]
| ^^^^ help: did you mean: `test`
error: 'test' may be misspelled as 'tests'
--> tests/ui/cfg_features.rs:25:15
|
LL | #[cfg(all(tests, Test))]
| ^^^^^ help: did you mean: `test`
error: 'test' may be misspelled as 'Test'
--> tests/ui/cfg_features.rs:25:22
|
LL | #[cfg(all(tests, Test))]
| ^^^^ help: did you mean: `test`
error: aborting due to 8 previous errors

View file

@ -18,5 +18,6 @@
#![warn(clippy::filter_map)] #![warn(clippy::filter_map)]
#![warn(clippy::pub_enum_variant_names)] #![warn(clippy::pub_enum_variant_names)]
#![warn(clippy::wrong_pub_self_convention)] #![warn(clippy::wrong_pub_self_convention)]
#![warn(clippy::maybe_misused_cfg)]
fn main() {} fn main() {}

View file

@ -97,5 +97,11 @@ error: lint `clippy::wrong_pub_self_convention` has been removed: set the `avoid
LL | #![warn(clippy::wrong_pub_self_convention)] LL | #![warn(clippy::wrong_pub_self_convention)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 16 previous errors error: lint `clippy::maybe_misused_cfg` has been removed: this lint has been replaced by `unexpected_cfgs`
--> tests/ui/deprecated.rs:21:9
|
LL | #![warn(clippy::maybe_misused_cfg)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 17 previous errors

View file

@ -5,18 +5,18 @@
//~^ ERROR: no need to put clippy lints behind a `clippy` cfg //~^ ERROR: no need to put clippy lints behind a `clippy` cfg
#![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))] #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
//~^ ERROR: no need to put clippy lints behind a `clippy` cfg //~^ ERROR: no need to put clippy lints behind a `clippy` cfg
#![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
//~^ ERROR: no need to put clippy lints behind a `clippy` cfg //~^ ERROR: no need to put clippy lints behind a `clippy` cfg
#![cfg_attr(clippy, deny(clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] #![cfg_attr(clippy, deny(clippy::non_minimal_cfg))]
//~^ ERROR: no need to put clippy lints behind a `clippy` cfg //~^ ERROR: no need to put clippy lints behind a `clippy` cfg
#[cfg_attr(clippy, deny(clippy::non_minimal_cfg))] #[cfg_attr(clippy, deny(clippy::non_minimal_cfg))]
//~^ ERROR: no need to put clippy lints behind a `clippy` cfg //~^ ERROR: no need to put clippy lints behind a `clippy` cfg
#[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))] #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
//~^ ERROR: no need to put clippy lints behind a `clippy` cfg //~^ ERROR: no need to put clippy lints behind a `clippy` cfg
#[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
//~^ ERROR: no need to put clippy lints behind a `clippy` cfg //~^ ERROR: no need to put clippy lints behind a `clippy` cfg
#[cfg_attr(clippy, deny(clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] #[cfg_attr(clippy, deny(clippy::non_minimal_cfg))]
//~^ ERROR: no need to put clippy lints behind a `clippy` cfg //~^ ERROR: no need to put clippy lints behind a `clippy` cfg
pub struct Bar; pub struct Bar;

View file

@ -18,16 +18,16 @@ LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
error: no need to put clippy lints behind a `clippy` cfg error: no need to put clippy lints behind a `clippy` cfg
--> tests/ui/unnecessary_clippy_cfg.rs:17:36 --> tests/ui/unnecessary_clippy_cfg.rs:17:36
| |
LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
| ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^
| |
= note: write instead: `#[deny(clippy::non_minimal_cfg,clippy::maybe_misused_cfg)]` = note: write instead: `#[deny(clippy::non_minimal_cfg)]`
error: no need to put clippy lints behind a `clippy` cfg error: no need to put clippy lints behind a `clippy` cfg
--> tests/ui/unnecessary_clippy_cfg.rs:19:1 --> tests/ui/unnecessary_clippy_cfg.rs:19:1
| |
LL | #[cfg_attr(clippy, deny(clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] LL | #[cfg_attr(clippy, deny(clippy::non_minimal_cfg))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `#[deny(clippy::non_minimal_cfg, clippy::maybe_misused_cfg)]` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `#[deny(clippy::non_minimal_cfg)]`
error: no need to put clippy lints behind a `clippy` cfg error: no need to put clippy lints behind a `clippy` cfg
--> tests/ui/unnecessary_clippy_cfg.rs:4:1 --> tests/ui/unnecessary_clippy_cfg.rs:4:1
@ -46,21 +46,21 @@ LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
error: no need to put clippy lints behind a `clippy` cfg error: no need to put clippy lints behind a `clippy` cfg
--> tests/ui/unnecessary_clippy_cfg.rs:8:37 --> tests/ui/unnecessary_clippy_cfg.rs:8:37
| |
LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
| ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^
| |
= note: write instead: `#![deny(clippy::non_minimal_cfg,clippy::maybe_misused_cfg)]` = note: write instead: `#![deny(clippy::non_minimal_cfg)]`
error: no need to put clippy lints behind a `clippy` cfg error: no need to put clippy lints behind a `clippy` cfg
--> tests/ui/unnecessary_clippy_cfg.rs:10:1 --> tests/ui/unnecessary_clippy_cfg.rs:10:1
| |
LL | #![cfg_attr(clippy, deny(clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] LL | #![cfg_attr(clippy, deny(clippy::non_minimal_cfg))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `#![deny(clippy::non_minimal_cfg, clippy::maybe_misused_cfg)]` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `#![deny(clippy::non_minimal_cfg)]`
error: duplicated attribute error: duplicated attribute
--> tests/ui/unnecessary_clippy_cfg.rs:8:26 --> tests/ui/unnecessary_clippy_cfg.rs:8:26
| |
LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
| ^^^^^^^^^ | ^^^^^^^^^
| |
note: first defined here note: first defined here
@ -71,7 +71,7 @@ LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
help: remove this attribute help: remove this attribute
--> tests/ui/unnecessary_clippy_cfg.rs:8:26 --> tests/ui/unnecessary_clippy_cfg.rs:8:26
| |
LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
| ^^^^^^^^^ | ^^^^^^^^^
= note: `-D clippy::duplicated-attributes` implied by `-D warnings` = note: `-D clippy::duplicated-attributes` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::duplicated_attributes)]` = help: to override `-D warnings` add `#[allow(clippy::duplicated_attributes)]`
@ -79,7 +79,7 @@ LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_
error: duplicated attribute error: duplicated attribute
--> tests/ui/unnecessary_clippy_cfg.rs:17:25 --> tests/ui/unnecessary_clippy_cfg.rs:17:25
| |
LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
| ^^^^^^^^^ | ^^^^^^^^^
| |
note: first defined here note: first defined here
@ -90,7 +90,7 @@ LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
help: remove this attribute help: remove this attribute
--> tests/ui/unnecessary_clippy_cfg.rs:17:25 --> tests/ui/unnecessary_clippy_cfg.rs:17:25
| |
LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))] LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
| ^^^^^^^^^ | ^^^^^^^^^
error: aborting due to 10 previous errors error: aborting due to 10 previous errors