rust/clippy_lints/src/empty_enum.rs

89 lines
3.5 KiB
Rust
Raw Normal View History

//! lint when there is an enum with no variants
use clippy_utils::diagnostics::span_lint_and_help;
2020-02-21 09:39:38 +01:00
use rustc_hir::{Item, ItemKind};
2020-01-12 15:08:41 +09:00
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
2018-03-28 15:24:26 +02:00
declare_clippy_lint! {
/// ### What it does
/// Checks for `enum`s with no variants, which therefore are uninhabited types
/// (cannot be instantiated).
///
/// As of this writing, the `never_type` is still a nightly-only experimental API.
/// Therefore, this lint is only triggered if `#![feature(never_type)]` is enabled.
///
/// ### Why is this bad?
/// * If you only want a type which cant be instantiated, you should use [`!`]
/// (the primitive type "never"), because [`!`] has more extensive compiler support
/// (type inference, etc.) and implementations of common traits.
2020-01-24 14:37:16 +03:00
///
/// * If you need to introduce a distinct type, consider using a [newtype] `struct`
/// containing [`!`] instead (`struct MyType(pub !)`), because it is more idiomatic
/// to use a `struct` rather than an `enum` when an `enum` is unnecessary.
///
/// If you do this, note that the [visibility] of the [`!`] field determines whether
/// the uninhabitedness is visible in documentation, and whether it can be pattern
/// matched to mark code unreachable. If the field is not visible, then the struct
/// acts like any other struct with private fields.
///
/// * If the enum has no variants only because all variants happen to be
/// [disabled by conditional compilation][cfg], then it would be appropriate
/// to allow the lint, with `#[allow(empty_enum)]`.
///
/// For further information, visit
/// [the never types documentation][`!`].
///
/// ### Example
2023-10-23 13:49:18 +00:00
/// ```no_run
/// enum CannotExist {}
/// ```
2020-01-24 14:37:16 +03:00
///
/// Use instead:
2023-10-23 13:49:18 +00:00
/// ```no_run
2020-01-24 14:37:16 +03:00
/// #![feature(never_type)]
///
/// /// Use the `!` type directly...
/// type CannotExist = !;
///
/// /// ...or define a newtype which is distinct.
/// struct CannotExist2(pub !);
2020-01-24 14:37:16 +03:00
/// ```
///
/// [`!`]: https://doc.rust-lang.org/std/primitive.never.html
/// [cfg]: https://doc.rust-lang.org/reference/conditional-compilation.html
/// [newtype]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#using-the-newtype-pattern-for-type-safety-and-abstraction
/// [visibility]: https://doc.rust-lang.org/reference/visibility-and-privacy.html
#[clippy::version = "pre 1.29.0"]
pub EMPTY_ENUM,
2018-03-28 15:24:26 +02:00
pedantic,
"enum with no variants"
}
2019-04-08 13:43:55 -07:00
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;
}
2019-09-27 17:16:06 +02:00
if let ItemKind::Enum(..) = item.kind {
let ty = cx.tcx.type_of(item.owner_id).instantiate_identity();
2018-11-27 21:14:15 +01:00
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
if adt.variants().is_empty() {
span_lint_and_help(
cx,
EMPTY_ENUM,
item.span,
"enum with no variants",
2020-04-19 20:38:07 +02:00
None,
"consider using the uninhabited type `!` (never type) or a wrapper \
around it to introduce a type which can't be instantiated",
);
}
}
}
}