RS.CLIPPY.FIELD_SCOPED_VISIBILITY_MODIFIERS
Checks for usage of a scoped visibility modifier, like `pub(crate)`, on fields
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: field_scoped_visibility_modifiers. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for usage of scoped visibility modifiers, like pub(crate), on fields. These
make a field visible within a scope between public and private.
Why restrict this?
Scoped visibility modifiers cause a field to be accessible within some scope between public and private, potentially within an entire crate. This allows for fields to be non-private while upholding internal invariants, but can be a code smell. Scoped visibility requires checking a greater area, potentially an entire crate, to verify that an invariant is upheld, and global analysis requires a lot of effort.
Example
pub mod public_module {
struct MyStruct {
pub(crate) first_field: bool,
pub(super) second_field: bool
}
}
Use instead:
pub mod public_module {
struct MyStruct {
first_field: bool,
second_field: bool
}
impl MyStruct {
pub(crate) fn get_first_field(&self) -> bool {
self.first_field
}
pub(super) fn get_second_field(&self) -> bool {
self.second_field
}
}
}