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