Skip to content

partial_pub_fields: disallow partial fields of a struct be pub #9604

@TennyZhuang

Description

@TennyZhuang
Contributor

What it does

https://matklad.github.io//2022/05/29/binary-privacy.html

Most structs are either ADT or Data, they shouldn't have a non-empty subset of pub fields.

The rule can be default allowed, since it may break some public APIs.

For pub(crate) or pub(mod path), it's commonly used as a friend class alternative, so I think we can treat them as private. If there are some requirements, we can add an option strict_partial_pub_fields later.

We can also introduce partial_pub_variants lint for enums later.

The lint can't be fixed automatically, since we don't know a struct is ADT or Data.

Lint Name

partial_pub_fields

Category

restriction

Advantage

  • Make a clear boundary of ADT or Data

Drawbacks

  • May break some published APIs

Example

#[derive(Default)]
pub struct FileSet {
  pub files: HashMap<VfsPath, FileId>,
  paths: HashMap<FileId, VfsPath>,
}

Could be written as:

#[derive(Default)]
pub struct FileSet {
  files: HashMap<VfsPath, FileId>,
  paths: HashMap<FileId, VfsPath>,
}
pub struct Color {
    pub r,
    pub g,
    b,
}

Could be written as:

pub struct Color {
    pub r,
    pub g,
    pub b,
}

Activity

TennyZhuang

TennyZhuang commented on Oct 8, 2022

@TennyZhuang
ContributorAuthor

@rustbot claim

added a commit that references this issue on Oct 16, 2022
d917590
added a commit that references this issue on Oct 23, 2023
b29ae3a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @TennyZhuang

    Issue actions

      `partial_pub_fields`: disallow partial fields of a struct be pub · Issue #9604 · rust-lang/rust-clippy