Skip to content

Commit

Permalink
Auto merge of #6513 - nahuakang:fix/empty_enum_lint_never_type, r=fli…
Browse files Browse the repository at this point in the history
…p1995

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.
  • Loading branch information
bors committed Jan 5, 2021
2 parents dd52066 + a8d47b4 commit 311186b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
11 changes: 10 additions & 1 deletion clippy_lints/src/empty_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! {
/// **What it does:** Checks for `enum`s with no variants.
///
/// As of this writing, the `never_type` is still a
/// nightly-only experimental API. Therefore, this lint is only triggered
/// if the `never_type` is enabled.
///
/// **Why is this bad?** If you want to introduce a type which
/// can't be instantiated, you should use `!` (the never type),
/// can't be instantiated, you should use `!` (the primitive type "never"),
/// or a wrapper around it, because `!` has more extensive
/// compiler support (type inference, etc...) and wrappers
/// around it are the conventional way to define an uninhabited type.
Expand Down Expand Up @@ -40,6 +44,11 @@ declare_lint_pass!(EmptyEnum => [EMPTY_ENUM]);

impl<'tcx> LateLintPass<'tcx> for EmptyEnum {
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
// Only suggest the `never_type` if the feature is enabled
if !cx.tcx.features().never_type {
return;
}

let did = cx.tcx.hir().local_def_id(item.hir_id);
if let ItemKind::Enum(..) = item.kind {
let ty = cx.tcx.type_of(did);
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/empty_enum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(dead_code)]
#![warn(clippy::empty_enum)]

// Enable never type to test empty enum lint
#![feature(never_type)]
enum Empty {}

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/empty_enum.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: enum with no variants
--> $DIR/empty_enum.rs:4:1
--> $DIR/empty_enum.rs:5:1
|
LL | enum Empty {}
| ^^^^^^^^^^^^^
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/empty_enum_without_never_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![allow(dead_code)]
#![warn(clippy::empty_enum)]

// `never_type` is not enabled; this test has no stderr file
enum Empty {}

fn main() {}

0 comments on commit 311186b

Please sign in to comment.