Skip to content

Commit 311186b

Browse files
committed
Auto merge of #6513 - nahuakang:fix/empty_enum_lint_never_type, r=flip1995
Fix: Empty enum never type suggested only if the feature is enabled This PR addresses [Issue 6422](#6422). Instead of always recommending `never type` for empty enums, Clippy would only recommend [the lint](https://rust-lang.github.io/rust-clippy/master/index.html#empty_enum) if [LatePass.TyCtxt](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html) has `features().never_type` enabled. - \[ ] Followed [lint naming conventions][lint_naming] - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[x] Added lint documentation - \[x] Run `cargo dev fmt` --- *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: Only trigger [`empty_enum`] lint if `never_type` feature is enabled.
2 parents dd52066 + a8d47b4 commit 311186b

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

clippy_lints/src/empty_enum.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
88
declare_clippy_lint! {
99
/// **What it does:** Checks for `enum`s with no variants.
1010
///
11+
/// As of this writing, the `never_type` is still a
12+
/// nightly-only experimental API. Therefore, this lint is only triggered
13+
/// if the `never_type` is enabled.
14+
///
1115
/// **Why is this bad?** If you want to introduce a type which
12-
/// can't be instantiated, you should use `!` (the never type),
16+
/// can't be instantiated, you should use `!` (the primitive type "never"),
1317
/// or a wrapper around it, because `!` has more extensive
1418
/// compiler support (type inference, etc...) and wrappers
1519
/// around it are the conventional way to define an uninhabited type.
@@ -40,6 +44,11 @@ declare_lint_pass!(EmptyEnum => [EMPTY_ENUM]);
4044

4145
impl<'tcx> LateLintPass<'tcx> for EmptyEnum {
4246
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
47+
// Only suggest the `never_type` if the feature is enabled
48+
if !cx.tcx.features().never_type {
49+
return;
50+
}
51+
4352
let did = cx.tcx.hir().local_def_id(item.hir_id);
4453
if let ItemKind::Enum(..) = item.kind {
4554
let ty = cx.tcx.type_of(did);

tests/ui/empty_enum.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(dead_code)]
22
#![warn(clippy::empty_enum)]
3-
3+
// Enable never type to test empty enum lint
4+
#![feature(never_type)]
45
enum Empty {}
56

67
fn main() {}

tests/ui/empty_enum.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: enum with no variants
2-
--> $DIR/empty_enum.rs:4:1
2+
--> $DIR/empty_enum.rs:5:1
33
|
44
LL | enum Empty {}
55
| ^^^^^^^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![allow(dead_code)]
2+
#![warn(clippy::empty_enum)]
3+
4+
// `never_type` is not enabled; this test has no stderr file
5+
enum Empty {}
6+
7+
fn main() {}

0 commit comments

Comments
 (0)