]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/empty_enum.rs
Rustfmt
[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 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_lint! {
20     pub EMPTY_ENUM,
21     Allow,
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().expect(
40                 "already checked whether this is an enum",
41             );
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 }