fix(empty_enum): don't lint if all variants happen to be cfg-d out (#15911)

Fixes https://github.com/rust-lang/rust-clippy/issues/15910

changelog: [`empty_enum`]: don't lint if all variants happen to be
`cfg`-d out
This commit is contained in:
Samuel Tardieu 2025-10-20 05:11:40 +00:00 committed by GitHub
commit 1ac3cc1a83
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 9 deletions

View file

@ -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 types documentation][`!`].
///
@ -62,11 +59,11 @@ 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_contains_cfg(cx, item.span)
{
span_lint_and_help(
cx,

View file

@ -1,8 +1,25 @@
#![allow(dead_code)]
#![warn(clippy::empty_enum)]
// Enable never type to test empty enum lint
#![feature(never_type)]
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() {}

View file

@ -1,6 +1,5 @@
//@ check-pass
#![allow(dead_code)]
#![warn(clippy::empty_enum)]
// `never_type` is not enabled; this test has no stderr file