2022-10-16 16:02:23 +08:00
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2022-10-16 16:21:48 +08:00
|
|
|
|
use rustc_ast::ast::{Item, ItemKind};
|
2022-10-16 16:02:23 +08:00
|
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
2023-11-25 17:45:27 +00:00
|
|
|
|
use rustc_session::declare_lint_pass;
|
2022-10-16 16:02:23 +08:00
|
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
|
/// ### What it does
|
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-22 22:21:01 -07:00
|
|
|
|
/// Checks whether some but not all fields of a `struct` are public.
|
2022-10-16 16:02:23 +08:00
|
|
|
|
///
|
|
|
|
|
/// Either make all fields of a type public, or make none of them public
|
|
|
|
|
///
|
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-22 22:21:01 -07:00
|
|
|
|
/// ### Why restrict this?
|
2022-10-16 16:02:23 +08:00
|
|
|
|
/// Most types should either be:
|
|
|
|
|
/// * Abstract data types: complex objects with opaque implementation which guard
|
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-22 22:21:01 -07:00
|
|
|
|
/// interior invariants and expose intentionally limited API to the outside world.
|
|
|
|
|
/// * Data: relatively simple objects which group a bunch of related attributes together,
|
|
|
|
|
/// but have no invariants.
|
2022-10-16 16:02:23 +08:00
|
|
|
|
///
|
|
|
|
|
/// ### Example
|
2023-10-23 13:49:18 +00:00
|
|
|
|
/// ```no_run
|
2022-10-16 16:02:23 +08:00
|
|
|
|
/// pub struct Color {
|
2022-10-16 17:10:27 +08:00
|
|
|
|
/// pub r: u8,
|
|
|
|
|
/// pub g: u8,
|
|
|
|
|
/// b: u8,
|
2022-10-16 16:02:23 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
/// Use instead:
|
2023-10-23 13:49:18 +00:00
|
|
|
|
/// ```no_run
|
2022-10-16 16:02:23 +08:00
|
|
|
|
/// pub struct Color {
|
2022-10-16 17:10:27 +08:00
|
|
|
|
/// pub r: u8,
|
|
|
|
|
/// pub g: u8,
|
|
|
|
|
/// pub b: u8,
|
2022-10-16 16:02:23 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
#[clippy::version = "1.66.0"]
|
|
|
|
|
pub PARTIAL_PUB_FIELDS,
|
|
|
|
|
restriction,
|
|
|
|
|
"partial fields of a struct are public"
|
|
|
|
|
}
|
|
|
|
|
declare_lint_pass!(PartialPubFields => [PARTIAL_PUB_FIELDS]);
|
|
|
|
|
|
|
|
|
|
impl EarlyLintPass for PartialPubFields {
|
|
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
|
|
|
|
let ItemKind::Struct(ref st, _) = item.kind else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut fields = st.fields().iter();
|
|
|
|
|
let Some(first_field) = fields.next() else {
|
|
|
|
|
// Empty struct.
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let all_pub = first_field.vis.kind.is_pub();
|
|
|
|
|
let all_priv = !all_pub;
|
|
|
|
|
|
|
|
|
|
let msg = "mixed usage of pub and non-pub fields";
|
|
|
|
|
|
|
|
|
|
for field in fields {
|
|
|
|
|
if all_priv && field.vis.kind.is_pub() {
|
|
|
|
|
span_lint_and_help(
|
|
|
|
|
cx,
|
2022-10-16 16:21:48 +08:00
|
|
|
|
PARTIAL_PUB_FIELDS,
|
2022-10-16 16:02:23 +08:00
|
|
|
|
field.vis.span,
|
|
|
|
|
msg,
|
|
|
|
|
None,
|
|
|
|
|
"consider using private field here",
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
} else if all_pub && !field.vis.kind.is_pub() {
|
|
|
|
|
span_lint_and_help(
|
|
|
|
|
cx,
|
2022-10-16 16:21:48 +08:00
|
|
|
|
PARTIAL_PUB_FIELDS,
|
2022-10-16 16:02:23 +08:00
|
|
|
|
field.vis.span,
|
|
|
|
|
msg,
|
|
|
|
|
None,
|
|
|
|
|
"consider using public field here",
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|