2017-02-04 21:07:54 -07:00
|
|
|
|
//! lint when there is an enum with no variants
|
|
|
|
|
|
2021-03-25 19:29:11 +01:00
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2020-02-21 09:39:38 +01:00
|
|
|
|
use rustc_hir::{Item, ItemKind};
|
2020-01-12 15:08:41 +09:00
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-11-25 17:45:27 +00:00
|
|
|
|
use rustc_session::declare_lint_pass;
|
2017-02-04 21:07:54 -07:00
|
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02:00
|
|
|
|
/// ### What it does
|
2024-05-22 18:03:18 -07:00
|
|
|
|
/// Checks for `enum`s with no variants, which therefore are uninhabited types
|
|
|
|
|
/// (cannot be instantiated).
|
2019-03-05 11:50:33 -05:00
|
|
|
|
///
|
2024-05-22 18:03:18 -07:00
|
|
|
|
/// As of this writing, the `never_type` is still a nightly-only experimental API.
|
|
|
|
|
/// Therefore, this lint is only triggered if `#![feature(never_type)]` is enabled.
|
2021-01-15 10:56:44 +01:00
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
|
/// ### Why is this bad?
|
2024-05-22 18:03:18 -07:00
|
|
|
|
/// * If you only want a type which can’t be instantiated, you should use [`!`]
|
|
|
|
|
/// (the primitive type "never"), because [`!`] has more extensive compiler support
|
|
|
|
|
/// (type inference, etc.) and implementations of common traits.
|
2020-01-24 14:37:16 +03:00
|
|
|
|
///
|
2024-05-22 18:03:18 -07:00
|
|
|
|
/// * If you need to introduce a distinct type, consider using a [newtype] `struct`
|
|
|
|
|
/// containing [`!`] instead (`struct MyType(pub !)`), because it is more idiomatic
|
|
|
|
|
/// to use a `struct` rather than an `enum` when an `enum` is unnecessary.
|
|
|
|
|
///
|
|
|
|
|
/// If you do this, note that the [visibility] of the [`!`] field determines whether
|
|
|
|
|
/// the uninhabitedness is visible in documentation, and whether it can be pattern
|
|
|
|
|
/// matched to mark code unreachable. If the field is not visible, then the struct
|
|
|
|
|
/// acts like any other struct with private fields.
|
|
|
|
|
///
|
|
|
|
|
/// * If the enum has no variants only because all variants happen to be
|
|
|
|
|
/// [disabled by conditional compilation][cfg], then it would be appropriate
|
|
|
|
|
/// to allow the lint, with `#[allow(empty_enum)]`.
|
|
|
|
|
///
|
|
|
|
|
/// For further information, visit
|
|
|
|
|
/// [the never type’s documentation][`!`].
|
2019-03-05 11:50:33 -05:00
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
|
/// ### Example
|
2023-10-23 13:49:18 +00:00
|
|
|
|
/// ```no_run
|
2024-05-22 18:03:18 -07:00
|
|
|
|
/// enum CannotExist {}
|
2019-03-05 11:50:33 -05:00
|
|
|
|
/// ```
|
2020-01-24 14:37:16 +03:00
|
|
|
|
///
|
2022-06-04 13:34:07 +02:00
|
|
|
|
/// Use instead:
|
2023-10-23 13:49:18 +00:00
|
|
|
|
/// ```no_run
|
2020-01-24 14:37:16 +03:00
|
|
|
|
/// #![feature(never_type)]
|
|
|
|
|
///
|
2024-05-22 18:03:18 -07:00
|
|
|
|
/// /// Use the `!` type directly...
|
|
|
|
|
/// type CannotExist = !;
|
|
|
|
|
///
|
|
|
|
|
/// /// ...or define a newtype which is distinct.
|
|
|
|
|
/// struct CannotExist2(pub !);
|
2020-01-24 14:37:16 +03:00
|
|
|
|
/// ```
|
2024-05-22 18:03:18 -07:00
|
|
|
|
///
|
|
|
|
|
/// [`!`]: https://doc.rust-lang.org/std/primitive.never.html
|
|
|
|
|
/// [cfg]: https://doc.rust-lang.org/reference/conditional-compilation.html
|
|
|
|
|
/// [newtype]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#using-the-newtype-pattern-for-type-safety-and-abstraction
|
|
|
|
|
/// [visibility]: https://doc.rust-lang.org/reference/visibility-and-privacy.html
|
2021-12-06 12:33:31 +01:00
|
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2017-02-04 21:07:54 -07:00
|
|
|
|
pub EMPTY_ENUM,
|
2018-03-28 15:24:26 +02:00
|
|
|
|
pedantic,
|
2017-02-04 21:07:54 -07:00
|
|
|
|
"enum with no variants"
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 13:43:55 -07:00
|
|
|
|
declare_lint_pass!(EmptyEnum => [EMPTY_ENUM]);
|
2017-02-04 21:07:54 -07:00
|
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for EmptyEnum {
|
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
2021-01-15 10:56:44 +01:00
|
|
|
|
// Only suggest the `never_type` if the feature is enabled
|
|
|
|
|
if !cx.tcx.features().never_type {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 17:16:06 +02:00
|
|
|
|
if let ItemKind::Enum(..) = item.kind {
|
2023-07-11 22:35:29 +01:00
|
|
|
|
let ty = cx.tcx.type_of(item.owner_id).instantiate_identity();
|
2018-11-27 21:14:15 +01:00
|
|
|
|
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
|
2022-03-05 07:28:41 +11:00
|
|
|
|
if adt.variants().is_empty() {
|
2020-04-18 18:28:29 +08:00
|
|
|
|
span_lint_and_help(
|
2020-04-17 22:01:25 +08:00
|
|
|
|
cx,
|
|
|
|
|
EMPTY_ENUM,
|
|
|
|
|
item.span,
|
|
|
|
|
"enum with no variants",
|
2020-04-19 20:38:07 +02:00
|
|
|
|
None,
|
2020-04-18 18:28:29 +08:00
|
|
|
|
"consider using the uninhabited type `!` (never type) or a wrapper \
|
|
|
|
|
around it to introduce a type which can't be instantiated",
|
2020-04-17 22:01:25 +08:00
|
|
|
|
);
|
2017-02-04 21:07:54 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|