Skip to content

Commit 1ac3cc1

Browse files
fix(empty_enum): don't lint if all variants happen to be cfg-d out (rust-lang#15911)
Fixes rust-lang/rust-clippy#15910 changelog: [`empty_enum`]: don't lint if all variants happen to be `cfg`-d out
2 parents 1ecb18a + c8c23bc commit 1ac3cc1

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

clippy_lints/src/empty_enum.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::span_contains_cfg;
23
use rustc_hir::{Item, ItemKind};
34
use rustc_lint::{LateContext, LateLintPass};
45
use rustc_session::declare_lint_pass;
@@ -25,10 +26,6 @@ declare_clippy_lint! {
2526
/// matched to mark code unreachable. If the field is not visible, then the struct
2627
/// acts like any other struct with private fields.
2728
///
28-
/// * If the enum has no variants only because all variants happen to be
29-
/// [disabled by conditional compilation][cfg], then it would be appropriate
30-
/// to allow the lint, with `#[allow(empty_enum)]`.
31-
///
3229
/// For further information, visit
3330
/// [the never type’s documentation][`!`].
3431
///
@@ -62,11 +59,11 @@ declare_lint_pass!(EmptyEnum => [EMPTY_ENUM]);
6259

6360
impl LateLintPass<'_> for EmptyEnum {
6461
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
65-
if let ItemKind::Enum(..) = item.kind
62+
if let ItemKind::Enum(.., def) = item.kind
63+
&& def.variants.is_empty()
6664
// Only suggest the `never_type` if the feature is enabled
6765
&& cx.tcx.features().never_type()
68-
&& let Some(adt) = cx.tcx.type_of(item.owner_id).instantiate_identity().ty_adt_def()
69-
&& adt.variants().is_empty()
66+
&& !span_contains_cfg(cx, item.span)
7067
{
7168
span_lint_and_help(
7269
cx,

tests/ui/empty_enum.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
#![allow(dead_code)]
21
#![warn(clippy::empty_enum)]
32
// Enable never type to test empty enum lint
43
#![feature(never_type)]
4+
55
enum Empty {}
66
//~^ empty_enum
77

8+
mod issue15910 {
9+
enum NotReallyEmpty {
10+
#[cfg(false)]
11+
Hidden,
12+
}
13+
14+
enum OneVisibleVariant {
15+
#[cfg(false)]
16+
Hidden,
17+
Visible,
18+
}
19+
20+
enum CfgInsideVariant {
21+
Variant(#[cfg(false)] String),
22+
}
23+
}
24+
825
fn main() {}

tests/ui/empty_enum_without_never_type.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@ check-pass
22

3-
#![allow(dead_code)]
43
#![warn(clippy::empty_enum)]
54

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

0 commit comments

Comments
 (0)