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 3 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
8 changes: 7 additions & 1 deletion clippy_lints/src/empty_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ 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
nahuakang marked this conversation as resolved.
Show resolved Hide resolved
///
/// **Why is this bad?** If you want to introduce a type which
/// can't be instantiated, you should use `!` (the never type),
/// or a wrapper around it, because `!` has more extensive
Expand Down Expand Up @@ -44,7 +48,9 @@ impl<'tcx> LateLintPass<'tcx> for EmptyEnum {
if let ItemKind::Enum(..) = item.kind {
let ty = cx.tcx.type_of(did);
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
if adt.variants.is_empty() {

// Only suggest the never type if the feature is enabled
if adt.variants.is_empty() && cx.tcx.features().never_type {
nahuakang marked this conversation as resolved.
Show resolved Hide resolved
span_lint_and_help(
cx,
EMPTY_ENUM,
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