1 //! lint when there is an enum with no variants
3 use clippy_utils::diagnostics::span_lint_and_help;
4 use rustc_hir::{Item, ItemKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 /// Checks for `enum`s with no variants.
12 /// As of this writing, the `never_type` is still a
13 /// nightly-only experimental API. Therefore, this lint is only triggered
14 /// if the `never_type` is enabled.
16 /// ### Why is this bad?
17 /// If you want to introduce a type which
18 /// can't be instantiated, you should use `!` (the primitive type "never"),
19 /// or a wrapper around it, because `!` has more extensive
20 /// compiler support (type inference, etc...) and wrappers
21 /// around it are the conventional way to define an uninhabited type.
22 /// For further information visit [never type documentation](https://doc.rust-lang.org/std/primitive.never.html)
33 /// #![feature(never_type)]
37 #[clippy::version = "pre 1.29.0"]
40 "enum with no variants"
43 declare_lint_pass!(EmptyEnum => [EMPTY_ENUM]);
45 impl<'tcx> LateLintPass<'tcx> for EmptyEnum {
46 fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
47 // Only suggest the `never_type` if the feature is enabled
48 if !cx.tcx.features().never_type {
52 if let ItemKind::Enum(..) = item.kind {
53 let ty = cx.tcx.type_of(item.def_id);
54 let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
55 if adt.variants.is_empty() {
60 "enum with no variants",
62 "consider using the uninhabited type `!` (never type) or a wrapper \
63 around it to introduce a type which can't be instantiated",