Email me

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-modifiers

The [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.

Restrictiongroup
allowdefault level
nofixed by cargo clippy --fix
1.81.0added in Rust

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

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ