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.
11 //! lint when there is an enum with no variants
13 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::rustc::hir::*;
16 use crate::utils::span_lint_and_then;
18 /// **What it does:** Checks for `enum`s with no variants.
20 /// **Why is this bad?** Enum's with no variants should be replaced with `!`,
21 /// the uninhabited type,
22 /// or a wrapper around it.
24 /// **Known problems:** None.
30 declare_clippy_lint! {
33 "enum with no variants"
36 #[derive(Copy, Clone)]
39 impl LintPass for EmptyEnum {
40 fn get_lints(&self) -> LintArray {
41 lint_array!(EMPTY_ENUM)
45 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
46 fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item) {
47 let did = cx.tcx.hir.local_def_id(item.id);
48 if let ItemKind::Enum(..) = item.node {
49 let ty = cx.tcx.type_of(did);
50 let adt = ty.ty_adt_def()
51 .expect("already checked whether this is an enum");
52 if adt.variants.is_empty() {
53 span_lint_and_then(cx, EMPTY_ENUM, item.span, "enum with no variants", |db| {
54 db.span_help(item.span, "consider using the uninhabited type `!` or a wrapper around it");