Skip to content

Commit cfa150b

Browse files
committed
Rephrase and expand empty_enum documentation.
* Remove incorrect claim that “wrappers around it are the conventional way to define an uninhabited type”. * Discuss why one would use `!`, a newtype struct, or keep the enum. * Add links to relevant documentation.
1 parent 05c4053 commit cfa150b

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

clippy_lints/src/empty_enum.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,53 @@ use rustc_session::declare_lint_pass;
77

88
declare_clippy_lint! {
99
/// ### 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).
1112
///
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.
1515
///
1616
/// ### 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.
2320
///
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][`!`].
2436
///
2537
/// ### Example
2638
/// ```no_run
27-
/// enum Test {}
39+
/// enum CannotExist {}
2840
/// ```
2941
///
3042
/// Use instead:
3143
/// ```no_run
3244
/// #![feature(never_type)]
3345
///
34-
/// struct Test(!);
46+
/// /// Use the `!` type directly...
47+
/// type CannotExist = !;
48+
///
49+
/// /// ...or define a newtype which is distinct.
50+
/// struct CannotExist2(pub !);
3551
/// ```
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
3657
#[clippy::version = "pre 1.29.0"]
3758
pub EMPTY_ENUM,
3859
pedantic,

0 commit comments

Comments
 (0)