From dbc76a766333bf510de601111f31e3eb42fb0434 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 19 May 2023 16:23:17 +0200 Subject: [PATCH] Add check for empty cfg `all` condition --- clippy_lints/src/attrs.rs | 8 ++++++++ tests/ui/non_minimal_cfg.fixed | 3 +++ tests/ui/non_minimal_cfg.rs | 3 +++ tests/ui/non_minimal_cfg2.rs | 6 ++++++ tests/ui/non_minimal_cfg2.stderr | 10 ++++++++++ 5 files changed, 30 insertions(+) create mode 100644 tests/ui/non_minimal_cfg2.rs create mode 100644 tests/ui/non_minimal_cfg2.stderr diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 6678002820a..897495ba108 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -796,6 +796,14 @@ fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) { } }, ); + } else if list.is_empty() && meta.has_name(sym::all) { + span_lint_and_then( + cx, + NON_MINIMAL_CFG, + meta.span, + "unneeded sub `cfg` when there is no condition", + |_| {}, + ); } } } diff --git a/tests/ui/non_minimal_cfg.fixed b/tests/ui/non_minimal_cfg.fixed index ca9a48a3e37..430caafb33e 100644 --- a/tests/ui/non_minimal_cfg.fixed +++ b/tests/ui/non_minimal_cfg.fixed @@ -11,4 +11,7 @@ fn wasi() {} #[cfg(all(unix, not(windows)))] fn the_end() {} +#[cfg(any())] +fn any() {} + fn main() {} diff --git a/tests/ui/non_minimal_cfg.rs b/tests/ui/non_minimal_cfg.rs index bf322236fef..a38ce1c21d6 100644 --- a/tests/ui/non_minimal_cfg.rs +++ b/tests/ui/non_minimal_cfg.rs @@ -11,4 +11,7 @@ fn wasi() {} #[cfg(all(any(unix), all(not(windows))))] fn the_end() {} +#[cfg(any())] +fn any() {} + fn main() {} diff --git a/tests/ui/non_minimal_cfg2.rs b/tests/ui/non_minimal_cfg2.rs new file mode 100644 index 00000000000..a4c6abce387 --- /dev/null +++ b/tests/ui/non_minimal_cfg2.rs @@ -0,0 +1,6 @@ +#![allow(unused)] + +#[cfg(all())] +fn all() {} + +fn main() {} diff --git a/tests/ui/non_minimal_cfg2.stderr b/tests/ui/non_minimal_cfg2.stderr new file mode 100644 index 00000000000..2a9a36fbcef --- /dev/null +++ b/tests/ui/non_minimal_cfg2.stderr @@ -0,0 +1,10 @@ +error: unneeded sub `cfg` when there is no condition + --> $DIR/non_minimal_cfg2.rs:3:7 + | +LL | #[cfg(all())] + | ^^^^^ + | + = note: `-D clippy::non-minimal-cfg` implied by `-D warnings` + +error: aborting due to previous error +