]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/empty_enum.rs
Merge pull request #1506 from bood/master
[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 `!`, the uninhabited type,
10 /// or a wrapper around it.
11 ///
12 /// **Known problems:** None.
13 ///
14 /// **Example:**
15 /// ```rust
16 /// enum Test {}
17 /// ```
18 declare_lint! {
19     pub EMPTY_ENUM,
20     Allow,
21     "enum with no variants"
22 }
23
24 #[derive(Copy,Clone)]
25 pub struct EmptyEnum;
26
27 impl LintPass for EmptyEnum {
28     fn get_lints(&self) -> LintArray {
29         lint_array!(EMPTY_ENUM)
30     }
31 }
32
33 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
34     fn check_item(&mut self, cx: &LateContext, item: &Item) {
35         let did = cx.tcx.hir.local_def_id(item.id);
36         if let ItemEnum(..) = item.node {
37             let ty = cx.tcx.item_type(did);
38             let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
39             if adt.variants.is_empty() {
40                 span_lint_and_then(cx, EMPTY_ENUM, item.span, "enum with no variants", |db| {
41                     db.span_help(item.span, "consider using the uninhabited type `!` or a wrapper around it");
42                 });
43             }
44         }
45     }
46 }