]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/empty_enum.rs
Merge pull request #3269 from rust-lang-nursery/relicense
[rust.git] / clippy_lints / src / empty_enum.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 //! lint when there is an enum with no variants
12
13 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::rustc::hir::*;
16 use crate::utils::span_lint_and_then;
17
18 /// **What it does:** Checks for `enum`s with no variants.
19 ///
20 /// **Why is this bad?** Enum's with no variants should be replaced with `!`,
21 /// the uninhabited type,
22 /// or a wrapper around it.
23 ///
24 /// **Known problems:** None.
25 ///
26 /// **Example:**
27 /// ```rust
28 /// enum Test {}
29 /// ```
30 declare_clippy_lint! {
31     pub EMPTY_ENUM,
32     pedantic,
33     "enum with no variants"
34 }
35
36 #[derive(Copy, Clone)]
37 pub struct EmptyEnum;
38
39 impl LintPass for EmptyEnum {
40     fn get_lints(&self) -> LintArray {
41         lint_array!(EMPTY_ENUM)
42     }
43 }
44
45 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
46     fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item) {
47         let did = cx.tcx.hir.local_def_id(item.id);
48         if let ItemKind::Enum(..) = item.node {
49             let ty = cx.tcx.type_of(did);
50             let adt = ty.ty_adt_def()
51                 .expect("already checked whether this is an enum");
52             if adt.variants.is_empty() {
53                 span_lint_and_then(cx, EMPTY_ENUM, item.span, "enum with no variants", |db| {
54                     db.span_help(item.span, "consider using the uninhabited type `!` or a wrapper around it");
55                 });
56             }
57         }
58     }
59 }