1 //! lint when there is an enum with no variants
3 use crate::utils::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};
9 /// **What it does:** Checks for `enum`s with no variants.
11 /// **Why is this bad?** If you want to introduce a type which
12 /// can't be instantiated, you should use `!` (the never type),
13 /// or a wrapper around it, because `!` has more extensive
14 /// compiler support (type inference, etc...) and wrappers
15 /// around it are the conventional way to define an uninhabited type.
16 /// For further information visit [never type documentation](https://doc.rust-lang.org/std/primitive.never.html)
19 /// **Known problems:** None.
30 /// #![feature(never_type)]
36 "enum with no variants"
39 declare_lint_pass!(EmptyEnum => [EMPTY_ENUM]);
41 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
42 fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item<'_>) {
43 let did = cx.tcx.hir().local_def_id(item.hir_id);
44 if let ItemKind::Enum(..) = item.kind {
45 let ty = cx.tcx.type_of(did);
46 let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
47 if adt.variants.is_empty() {
52 "enum with no variants",
54 "consider using the uninhabited type `!` (never type) or a wrapper \
55 around it to introduce a type which can't be instantiated",