You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
structDatabaseGuard{pub_db_handle:Handle,}
Could be written as:
// if you do not intend to use the field `_db_handle`structDatabaseGuard{_db_handle:Handle,}// ... or if you do intend to use the fieldstructDatabaseGuard{pubdb_handle:Handle,}
The text was updated successfully, but these errors were encountered:
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 -->
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 -->
What it does
This lint would warn users if any field in a struct is prefixed with an
_
and markedpub
, e.g.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
lintLint Name
pub_underscore_field
Category
pedantic
Advantage
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 bepub
. I could be missing scenarios though :)Example
Could be written as:
The text was updated successfully, but these errors were encountered: