1 //! lint when there is an enum with no variants
3 use crate::utils::span_lint_and_then;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_tool_lint, lint_array};
8 /// **What it does:** Checks for `enum`s with no variants.
10 /// **Why is this bad?** Enum's with no variants should be replaced with `!`,
11 /// the uninhabited type,
12 /// or a wrapper around it.
14 /// **Known problems:** None.
20 declare_clippy_lint! {
23 "enum with no variants"
26 #[derive(Copy, Clone)]
29 impl LintPass for EmptyEnum {
30 fn get_lints(&self) -> LintArray {
31 lint_array!(EMPTY_ENUM)
34 fn name(&self) -> &'static str {
39 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
40 fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item) {
41 let did = cx.tcx.hir().local_def_id_from_hir_id(item.hir_id);
42 if let ItemKind::Enum(..) = item.node {
43 let ty = cx.tcx.type_of(did);
44 let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
45 if adt.variants.is_empty() {
46 span_lint_and_then(cx, EMPTY_ENUM, item.span, "enum with no variants", |db| {
49 "consider using the uninhabited type `!` or a wrapper around it",