@@ -7,32 +7,53 @@ use rustc_session::declare_lint_pass;
7
7
8
8
declare_clippy_lint ! {
9
9
/// ### What it does
10
- /// Checks for `enum`s with no variants.
10
+ /// Checks for `enum`s with no variants, which therefore are uninhabited types
11
+ /// (cannot be instantiated).
11
12
///
12
- /// As of this writing, the `never_type` is still a
13
- /// nightly-only experimental API. Therefore, this lint is only triggered
14
- /// if the `never_type` is enabled.
13
+ /// As of this writing, the `never_type` is still a nightly-only experimental API.
14
+ /// Therefore, this lint is only triggered if `#![feature(never_type)]` is enabled.
15
15
///
16
16
/// ### Why is this bad?
17
- /// If you want to introduce a type which
18
- /// can't be instantiated, you should use `!` (the primitive type "never"),
19
- /// or a wrapper around it, because `!` has more extensive
20
- /// compiler support (type inference, etc...) and wrappers
21
- /// around it are the conventional way to define an uninhabited type.
22
- /// For further information visit [never type documentation](https://doc.rust-lang.org/std/primitive.never.html)
17
+ /// * If you only want a type which can’t be instantiated, you should use [`!`]
18
+ /// (the primitive type "never"), because [`!`] has more extensive compiler support
19
+ /// (type inference, etc.) and implementations of common traits.
23
20
///
21
+ /// * If you need to introduce a distinct type, consider using a [newtype] `struct`
22
+ /// containing [`!`] instead (`struct MyType(pub !)`), because it is more idiomatic
23
+ /// to use a `struct` rather than an `enum` when an `enum` is unnecessary.
24
+ ///
25
+ /// If you do this, note that the [visibility] of the [`!`] field determines whether
26
+ /// the uninhabitedness is visible in documentation, and whether it can be pattern
27
+ /// matched to mark code unreachable. If the field is not visible, then the struct
28
+ /// acts like any other struct with private fields.
29
+ ///
30
+ /// * If the enum has no variants only because all variants happen to be
31
+ /// [disabled by conditional compilation][cfg], then it would be appropriate
32
+ /// to allow the lint, with `#[allow(empty_enum)]`.
33
+ ///
34
+ /// For further information, visit
35
+ /// [the never type’s documentation][`!`].
24
36
///
25
37
/// ### Example
26
38
/// ```no_run
27
- /// enum Test {}
39
+ /// enum CannotExist {}
28
40
/// ```
29
41
///
30
42
/// Use instead:
31
43
/// ```no_run
32
44
/// #![feature(never_type)]
33
45
///
34
- /// struct Test(!);
46
+ /// /// Use the `!` type directly...
47
+ /// type CannotExist = !;
48
+ ///
49
+ /// /// ...or define a newtype which is distinct.
50
+ /// struct CannotExist2(pub !);
35
51
/// ```
52
+ ///
53
+ /// [`!`]: https://doc.rust-lang.org/std/primitive.never.html
54
+ /// [cfg]: https://doc.rust-lang.org/reference/conditional-compilation.html
55
+ /// [newtype]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#using-the-newtype-pattern-for-type-safety-and-abstraction
56
+ /// [visibility]: https://doc.rust-lang.org/reference/visibility-and-privacy.html
36
57
#[ clippy:: version = "pre 1.29.0" ]
37
58
pub EMPTY_ENUM ,
38
59
pedantic,
0 commit comments