Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions clippy_lints/src/empty_enum.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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][`!`].
///
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 18 additions & 1 deletion tests/ui/empty_enum.rs
Original file line number Diff line number Diff line change
@@ -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() {}
1 change: 0 additions & 1 deletion tests/ui/empty_enum_without_never_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//@ check-pass

#![allow(dead_code)]
#![warn(clippy::empty_enum)]

// `never_type` is not enabled; this test has no stderr file
Expand Down