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](https://github.com/rust-lang/rust-clippy/issues/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.
This commit is contained in:
bors 2021-01-05 09:37:19 +00:00
commit 311186b9bb
4 changed files with 20 additions and 3 deletions

View File

@ -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.
@ -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);

View File

@ -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() {}

View File

@ -1,5 +1,5 @@
error: enum with no variants
--> $DIR/empty_enum.rs:4:1
--> $DIR/empty_enum.rs:5:1
|
LL | enum Empty {}
| ^^^^^^^^^^^^^

View File

@ -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() {}