1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 //! lint when there is an enum with no variants
12 use crate::rustc::hir::*;
13 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::utils::span_lint_and_then;
17 /// **What it does:** Checks for `enum`s with no variants.
19 /// **Why is this bad?** Enum's with no variants should be replaced with `!`,
20 /// the uninhabited type,
21 /// or a wrapper around it.
23 /// **Known problems:** None.
29 declare_clippy_lint! {
32 "enum with no variants"
35 #[derive(Copy, Clone)]
38 impl LintPass for EmptyEnum {
39 fn get_lints(&self) -> LintArray {
40 lint_array!(EMPTY_ENUM)
44 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
45 fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item) {
46 let did = cx.tcx.hir().local_def_id(item.id);
47 if let ItemKind::Enum(..) = item.node {
48 let ty = cx.tcx.type_of(did);
49 let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
50 if adt.variants.is_empty() {
51 span_lint_and_then(cx, EMPTY_ENUM, item.span, "enum with no variants", |db| {
54 "consider using the uninhabited type `!` or a wrapper around it",