]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/empty_enum.rs
Changes lint sugg to bitwise and operator `&`
[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 //! lint when there is an enum with no variants
11
12 use crate::rustc::hir::*;
13 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::utils::span_lint_and_then;
16
17 /// **What it does:** Checks for `enum`s with no variants.
18 ///
19 /// **Why is this bad?** Enum's with no variants should be replaced with `!`,
20 /// the uninhabited type,
21 /// or a wrapper around it.
22 ///
23 /// **Known problems:** None.
24 ///
25 /// **Example:**
26 /// ```rust
27 /// enum Test {}
28 /// ```
29 declare_clippy_lint! {
30     pub EMPTY_ENUM,
31     pedantic,
32     "enum with no variants"
33 }
34
35 #[derive(Copy, Clone)]
36 pub struct EmptyEnum;
37
38 impl LintPass for EmptyEnum {
39     fn get_lints(&self) -> LintArray {
40         lint_array!(EMPTY_ENUM)
41     }
42 }
43
44 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
45     fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item) {
46         let did = cx.tcx.hir().local_def_id(item.id);
47         if let ItemKind::Enum(..) = item.node {
48             let ty = cx.tcx.type_of(did);
49             let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
50             if adt.variants.is_empty() {
51                 span_lint_and_then(cx, EMPTY_ENUM, item.span, "enum with no variants", |db| {
52                     db.span_help(
53                         item.span,
54                         "consider using the uninhabited type `!` or a wrapper around it",
55                     );
56                 });
57             }
58         }
59     }
60 }