Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Empty enum never type suggested only if the feature is enabled #6513

Merged
merged 5 commits into from
Jan 5, 2021
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: 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)]
nahuakang marked this conversation as resolved.
Show resolved Hide resolved
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() {}