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