Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::field_scoped_visibility_modifiers: scoped visibility modifier on a field
What it meansChecks for usage of scoped visibility modifiers, like pub(crate), on fields. These make a field visible within a scope between public and private. It is in the restriction group and off by default.
What you see
warning: scoped visibility modifier on a field --> main.rs:5:9 | 5 | pub(crate) first_field: bool, | ^^^^^^^^^^ | = help: consider making the field private and adding a scoped visibility method for it = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#field_scoped_visibility_modifiers = note: requested on the command line with `-W clippy::field-scoped-visibility-modifiers`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
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.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
pub mod public_module {
struct MyStruct {
pub(crate) first_field: bool,
pub(super) second_field: bool
}
}
Write this 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
}
}
}
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
field_scoped_visibility_modifiers = "warn"In code
#![warn(clippy::field_scoped_visibility_modifiers)]One run
cargo clippy -- -W clippy::field-scoped-visibility-modifiersThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
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
}
}
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- exhaustive_structsWarns on any exported structs that are not tagged #[non_exhaustive]
- exitDetects calls to the exit() function that are not in the main function
- filetype_is_fileChecks for FileType::is_file()
- float_arithmeticChecks for float arithmetic
- float_cmp_conststrict comparison of `f32` or `f64` constant
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?