From 70de06f70fee073850b5b2129a9782b86ac505e0 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 20 Oct 2025 00:06:51 +0200 Subject: [PATCH 1/2] clean-up a bit --- clippy_lints/src/empty_enum.rs | 5 ++--- tests/ui/empty_enum.rs | 2 +- tests/ui/empty_enum_without_never_type.rs | 1 - 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/empty_enum.rs b/clippy_lints/src/empty_enum.rs index b0389fd9a2f6..2a0dde656682 100644 --- a/clippy_lints/src/empty_enum.rs +++ b/clippy_lints/src/empty_enum.rs @@ -62,11 +62,10 @@ declare_lint_pass!(EmptyEnum => [EMPTY_ENUM]); impl LateLintPass<'_> for EmptyEnum { fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { - if let ItemKind::Enum(..) = item.kind + if let ItemKind::Enum(.., def) = item.kind + && def.variants.is_empty() // Only suggest the `never_type` if the feature is enabled && cx.tcx.features().never_type() - && let Some(adt) = cx.tcx.type_of(item.owner_id).instantiate_identity().ty_adt_def() - && adt.variants().is_empty() { span_lint_and_help( cx, diff --git a/tests/ui/empty_enum.rs b/tests/ui/empty_enum.rs index 439fd0974f5f..ea5a00bb7843 100644 --- a/tests/ui/empty_enum.rs +++ b/tests/ui/empty_enum.rs @@ -1,7 +1,7 @@ -#![allow(dead_code)] #![warn(clippy::empty_enum)] // Enable never type to test empty enum lint #![feature(never_type)] + enum Empty {} //~^ empty_enum diff --git a/tests/ui/empty_enum_without_never_type.rs b/tests/ui/empty_enum_without_never_type.rs index 3661a1537208..cec53c555246 100644 --- a/tests/ui/empty_enum_without_never_type.rs +++ b/tests/ui/empty_enum_without_never_type.rs @@ -1,6 +1,5 @@ //@ check-pass -#![allow(dead_code)] #![warn(clippy::empty_enum)] // `never_type` is not enabled; this test has no stderr file From c8c23bcf5b1a4da39029577224a9be01860143fa Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 20 Oct 2025 00:14:14 +0200 Subject: [PATCH 2/2] fix(empty_enum): don't lint if all variants happen to be `cfg`-d out --- clippy_lints/src/empty_enum.rs | 6 ++---- tests/ui/empty_enum.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/empty_enum.rs b/clippy_lints/src/empty_enum.rs index 2a0dde656682..19871ac5c50f 100644 --- a/clippy_lints/src/empty_enum.rs +++ b/clippy_lints/src/empty_enum.rs @@ -1,4 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::span_contains_cfg; use rustc_hir::{Item, ItemKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; @@ -25,10 +26,6 @@ declare_clippy_lint! { /// matched to mark code unreachable. If the field is not visible, then the struct /// acts like any other struct with private fields. /// - /// * If the enum has no variants only because all variants happen to be - /// [disabled by conditional compilation][cfg], then it would be appropriate - /// to allow the lint, with `#[allow(empty_enum)]`. - /// /// For further information, visit /// [the never type’s documentation][`!`]. /// @@ -66,6 +63,7 @@ impl LateLintPass<'_> for EmptyEnum { && def.variants.is_empty() // Only suggest the `never_type` if the feature is enabled && cx.tcx.features().never_type() + && !span_contains_cfg(cx, item.span) { span_lint_and_help( cx, diff --git a/tests/ui/empty_enum.rs b/tests/ui/empty_enum.rs index ea5a00bb7843..009d8c430490 100644 --- a/tests/ui/empty_enum.rs +++ b/tests/ui/empty_enum.rs @@ -5,4 +5,21 @@ enum Empty {} //~^ empty_enum +mod issue15910 { + enum NotReallyEmpty { + #[cfg(false)] + Hidden, + } + + enum OneVisibleVariant { + #[cfg(false)] + Hidden, + Visible, + } + + enum CfgInsideVariant { + Variant(#[cfg(false)] String), + } +} + fn main() {}