1 use clippy_utils
::diagnostics
::span_lint_and_help
;
2 use rustc_ast
::ast
::{Item, ItemKind}
;
3 use rustc_lint
::{EarlyContext, EarlyLintPass}
;
4 use rustc_session
::declare_lint_pass
;
8 /// Checks whether partial fields of a struct are public.
10 /// Either make all fields of a type public, or make none of them public
12 /// ### Why is this bad?
13 /// Most types should either be:
14 /// * Abstract data types: complex objects with opaque implementation which guard
15 /// interior invariants and expose intentionally limited API to the outside world.
16 /// * Data: relatively simple objects which group a bunch of related attributes together.
20 /// pub struct Color {
28 /// pub struct Color {
34 #[clippy::version = "1.66.0"]
35 pub PARTIAL_PUB_FIELDS
,
37 "partial fields of a struct are public"
39 declare_lint_pass
!(PartialPubFields
=> [PARTIAL_PUB_FIELDS
]);
41 impl EarlyLintPass
for PartialPubFields
{
42 fn check_item(&mut self, cx
: &EarlyContext
<'_
>, item
: &Item
) {
43 let ItemKind
::Struct(ref st
, _
) = item
.kind
else {
47 let mut fields
= st
.fields().iter();
48 let Some(first_field
) = fields
.next() else {
52 let all_pub
= first_field
.vis
.kind
.is_pub();
53 let all_priv
= !all_pub
;
55 let msg
= "mixed usage of pub and non-pub fields";
58 if all_priv
&& field
.vis
.kind
.is_pub() {
65 "consider using private field here",
68 } else if all_pub
&& !field
.vis
.kind
.is_pub() {
75 "consider using public field here",