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