2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
|
|
|
use clippy_utils::source::indent_of;
|
2021-01-30 11:06:34 -06:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{Item, ItemKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-11-25 11:45:27 -06:00
|
|
|
use rustc_session::declare_lint_pass;
|
2021-01-30 11:06:34 -06:00
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Warns on any exported `enum`s that are not tagged `#[non_exhaustive]`
|
2021-01-30 11:06:34 -06:00
|
|
|
///
|
For restriction lints, replace “Why is this bad?” with “Why restrict this?”
The `restriction` group contains many lints which are not about
necessarily “bad” things, but style choices — perhaps even style choices
which contradict conventional Rust style — or are otherwise very
situational. This results in silly wording like “Why is this bad?
It isn't, but ...”, which I’ve seen confuse a newcomer at least once.
To improve this situation, this commit replaces the “Why is this bad?”
section heading with “Why restrict this?”, for most, but not all,
restriction lints. I left alone the ones whose placement in the
restriction group is more incidental.
In order to make this make sense, I had to remove the “It isn't, but”
texts from the contents of the sections. Sometimes further changes
were needed, or there were obvious fixes to make, and I went ahead
and made those changes without attempting to split them into another
commit, even though many of them are not strictly necessary for the
“Why restrict this?” project.
2024-05-23 00:21:01 -05:00
|
|
|
/// ### Why restrict this?
|
|
|
|
/// Making an `enum` exhaustive is a stability commitment: adding a variant is a breaking change.
|
|
|
|
/// A project may wish to ensure that there are no exhaustive enums or that every exhaustive
|
|
|
|
/// `enum` is explicitly `#[allow]`ed.
|
2021-01-30 11:06:34 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2023-10-23 08:49:18 -05:00
|
|
|
/// ```no_run
|
2021-01-30 11:06:34 -06:00
|
|
|
/// enum Foo {
|
|
|
|
/// Bar,
|
|
|
|
/// Baz
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-10-23 08:49:18 -05:00
|
|
|
/// ```no_run
|
2021-01-30 11:06:34 -06:00
|
|
|
/// #[non_exhaustive]
|
|
|
|
/// enum Foo {
|
|
|
|
/// Bar,
|
|
|
|
/// Baz
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.51.0"]
|
2021-01-30 11:06:34 -06:00
|
|
|
pub EXHAUSTIVE_ENUMS,
|
|
|
|
restriction,
|
|
|
|
"detects exported enums that have not been marked #[non_exhaustive]"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
2024-03-22 17:19:31 -05:00
|
|
|
/// Warns on any exported `struct`s that are not tagged `#[non_exhaustive]`
|
2021-01-30 11:06:34 -06:00
|
|
|
///
|
For restriction lints, replace “Why is this bad?” with “Why restrict this?”
The `restriction` group contains many lints which are not about
necessarily “bad” things, but style choices — perhaps even style choices
which contradict conventional Rust style — or are otherwise very
situational. This results in silly wording like “Why is this bad?
It isn't, but ...”, which I’ve seen confuse a newcomer at least once.
To improve this situation, this commit replaces the “Why is this bad?”
section heading with “Why restrict this?”, for most, but not all,
restriction lints. I left alone the ones whose placement in the
restriction group is more incidental.
In order to make this make sense, I had to remove the “It isn't, but”
texts from the contents of the sections. Sometimes further changes
were needed, or there were obvious fixes to make, and I went ahead
and made those changes without attempting to split them into another
commit, even though many of them are not strictly necessary for the
“Why restrict this?” project.
2024-05-23 00:21:01 -05:00
|
|
|
/// ### Why restrict this?
|
|
|
|
/// Making a `struct` exhaustive is a stability commitment: adding a field is a breaking change.
|
|
|
|
/// A project may wish to ensure that there are no exhaustive structs or that every exhaustive
|
|
|
|
/// `struct` is explicitly `#[allow]`ed.
|
2021-01-30 11:06:34 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2023-10-23 08:49:18 -05:00
|
|
|
/// ```no_run
|
2021-01-30 11:06:34 -06:00
|
|
|
/// struct Foo {
|
|
|
|
/// bar: u8,
|
|
|
|
/// baz: String,
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-10-23 08:49:18 -05:00
|
|
|
/// ```no_run
|
2021-01-30 11:06:34 -06:00
|
|
|
/// #[non_exhaustive]
|
|
|
|
/// struct Foo {
|
|
|
|
/// bar: u8,
|
|
|
|
/// baz: String,
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.51.0"]
|
2021-01-30 11:06:34 -06:00
|
|
|
pub EXHAUSTIVE_STRUCTS,
|
|
|
|
restriction,
|
|
|
|
"detects exported structs that have not been marked #[non_exhaustive]"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(ExhaustiveItems => [EXHAUSTIVE_ENUMS, EXHAUSTIVE_STRUCTS]);
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for ExhaustiveItems {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
2024-06-10 23:39:51 -05:00
|
|
|
let (lint, msg, fields) = match item.kind {
|
|
|
|
ItemKind::Enum(..) => (
|
|
|
|
EXHAUSTIVE_ENUMS,
|
|
|
|
"exported enums should not be exhaustive",
|
|
|
|
[].as_slice(),
|
|
|
|
),
|
|
|
|
ItemKind::Struct(v, ..) => (
|
|
|
|
EXHAUSTIVE_STRUCTS,
|
|
|
|
"exported structs should not be exhaustive",
|
|
|
|
v.fields(),
|
|
|
|
),
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
if cx.effective_visibilities.is_exported(item.owner_id.def_id)
|
2023-11-10 11:29:28 -06:00
|
|
|
&& let attrs = cx.tcx.hir().attrs(item.hir_id())
|
|
|
|
&& !attrs.iter().any(|a| a.has_name(sym::non_exhaustive))
|
2024-06-10 23:39:51 -05:00
|
|
|
&& fields.iter().all(|f| cx.tcx.visibility(f.def_id).is_public())
|
2023-11-10 11:29:28 -06:00
|
|
|
{
|
2023-11-02 14:23:36 -05:00
|
|
|
span_lint_and_then(cx, lint, item.span, msg, |diag| {
|
2024-07-21 04:00:02 -05:00
|
|
|
let suggestion_span = item.span.shrink_to_lo();
|
|
|
|
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));
|
2023-11-02 14:23:36 -05:00
|
|
|
let sugg = format!("#[non_exhaustive]\n{indent}");
|
2024-07-21 04:00:02 -05:00
|
|
|
diag.span_suggestion_verbose(
|
2023-11-02 14:23:36 -05:00
|
|
|
suggestion_span,
|
|
|
|
"try adding #[non_exhaustive]",
|
|
|
|
sugg,
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
});
|
2021-01-30 11:06:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|