]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/empty_enum.rs
Merge branch 'master' into issue-2879
[rust.git] / clippy_lints / src / empty_enum.rs
1 //! lint when there is an enum with no variants
2
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
5 use rustc::hir::*;
6 use crate::utils::span_lint_and_then;
7
8 /// **What it does:** Checks for `enum`s with no variants.
9 ///
10 /// **Why is this bad?** Enum's with no variants should be replaced with `!`,
11 /// the uninhabited type,
12 /// or a wrapper around it.
13 ///
14 /// **Known problems:** None.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// enum Test {}
19 /// ```
20 declare_clippy_lint! {
21     pub EMPTY_ENUM,
22     pedantic,
23     "enum with no variants"
24 }
25
26 #[derive(Copy, Clone)]
27 pub struct EmptyEnum;
28
29 impl LintPass for EmptyEnum {
30     fn get_lints(&self) -> LintArray {
31         lint_array!(EMPTY_ENUM)
32     }
33 }
34
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
36     fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item) {
37         let did = cx.tcx.hir.local_def_id(item.id);
38         if let ItemKind::Enum(..) = item.node {
39             let ty = cx.tcx.type_of(did);
40             let adt = ty.ty_adt_def()
41                 .expect("already checked whether this is an enum");
42             if adt.variants.is_empty() {
43                 span_lint_and_then(cx, EMPTY_ENUM, item.span, "enum with no variants", |db| {
44                     db.span_help(item.span, "consider using the uninhabited type `!` or a wrapper around it");
45                 });
46             }
47         }
48     }
49 }