Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ParkMyCar opened this issue Feb 3, 2023 · 0 comments · Fixed by #10283
Closed

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

ParkMyCar opened this issue Feb 3, 2023 · 0 comments · Fixed by #10283
Labels
A-lint Area: New lints

Comments

@ParkMyCar
Copy link
Contributor

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,
}
@ParkMyCar ParkMyCar added the A-lint Area: New lints label Feb 3, 2023
bors added a commit that referenced this issue Dec 27, 2023
feature: add new lint `pub_underscore_fields`

fixes: #10282

This PR introduces a new lint `pub_underscore_fields` that lints when a user has marked a field of a struct as public, but also prefixed it with an underscore (`_`). This is something users should avoid because the two ideas are contradictory. Prefixing a field with an `_` is inferred as the field being unused, but making a field public infers that it will be used.

- \[x] Followed [lint naming conventions][lint_naming]
  - I believe I followed the naming conventions, more than happy to update the naming if I did not :)
- \[x] Added passing UI tests (including committed `.stderr` file)
- \[x] `cargo test` passes locally
- \[x] Executed `cargo dev update_lints`
- \[x] Added lint documentation
- \[x] Run `cargo dev fmt`

---

changelog: new lint: [`pub_underscore_fields`]
[#10283](#10283)
<!-- changelog_checked -->
bors added a commit that referenced this issue Dec 29, 2023
feature: add new lint `pub_underscore_fields`

fixes: #10282

This PR introduces a new lint `pub_underscore_fields` that lints when a user has marked a field of a struct as public, but also prefixed it with an underscore (`_`). This is something users should avoid because the two ideas are contradictory. Prefixing a field with an `_` is inferred as the field being unused, but making a field public infers that it will be used.

- \[x] Followed [lint naming conventions][lint_naming]
  - I believe I followed the naming conventions, more than happy to update the naming if I did not :)
- \[x] Added passing UI tests (including committed `.stderr` file)
- \[x] `cargo test` passes locally
- \[x] Executed `cargo dev update_lints`
- \[x] Added lint documentation
- \[x] Run `cargo dev fmt`

---

changelog: new lint: [`pub_underscore_fields`]
[#10283](#10283)
<!-- changelog_checked -->
@bors bors closed this as completed in 174a0d7 Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant