Skip to content

feature: Warn for pub fields prefixed with an underscore #10282

Closed
@ParkMyCar

Description

@ParkMyCar

What it does

This lint would warn users if any field in a struct is prefixed with an _ and marked pub, e.g.

struct DatabaseGuard {
  pub _db_handle: Handle,
}

Prefixing a field name with an underscore is generally used to silence the unused field checker, so if you're suggesting the field shouldn't be used, then making it pub is most likely a mistake.

Note: this would be fairly similar to the used_underscore_binding lint

Lint Name

pub_underscore_field

Category

pedantic

Advantage

  • Promotes better "visibility hygiene"
  • Makes the concept of field access consistent across naming and visibility rules

Drawbacks

I don't think there are any. If someone has prefixed a field with an _ that shows intent to not use the field, and as such it doesn't/shouldn't need to be pub. I could be missing scenarios though :)

Example

struct DatabaseGuard {
  pub _db_handle: Handle,
}

Could be written as:

// if you do not intend to use the field `_db_handle`
struct DatabaseGuard {
  _db_handle: Handle,
}

// ... or if you do intend to use the field
struct DatabaseGuard {
  pub db_handle: Handle,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions