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