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
For some uses, pub fields are disadvantageous and error-prone. Selectively adding #[deny(pub_fields)] to a type would make these uses more distinct.
Other advantages come from using private fields, which may include:
Preventing external mutation of internal invariants
(doubly so for unsafe)
Easier debugging and refactoring due to encapsulated logic
In short, all field accesses are within the type itself.
Drawbacks
There are no drawbacks past making code somewhat more verbose (lint, getters, setters, etc.), but that stems from private fields in general.
Newer programmers may accidentally apply it when they may not wish to do so. I strongly suggest asking users to apply this lint on a 'per-type' basis.
Example
Let's say that MyStruct is a large type with a huuuge impl block and lots of unsafe code.
Watching field accesses is vital to maintain memory safety in this type, so pub fields are generally unwanted.
// note: dear future contributors...// please do not use `pub` fields in this type. encapsulation is// important for safety, and blah blah blah...pubstructMyStruct{some_fields:*mut(),// ...// let's say a contributor was loose with the rules. can also// occur with limited docs or in large projects!pubanother_field:u32,}
Could be written as:
#[deny(pub_fields, reason = "encapsulate safety invariants for fields")]pubstructMyStruct{some_fields:*mut(),// ...another_field:u32,}
The lint prevented potential changes that could affect memory safety. For example, another_field could be a computed offset that shouldn't be affected.
The text was updated successfully, but these errors were encountered:
What it does
Checks for
pub
fields on a type.Advantage
For some uses,
pub
fields are disadvantageous and error-prone. Selectively adding#[deny(pub_fields)]
to a type would make these uses more distinct.Other advantages come from using private fields, which may include:
unsafe
)In short, all field accesses are within the type itself.
Drawbacks
There are no drawbacks past making code somewhat more verbose (lint, getters, setters, etc.), but that stems from private fields in general.
Newer programmers may accidentally apply it when they may not wish to do so. I strongly suggest asking users to apply this lint on a 'per-type' basis.
Example
Let's say that
MyStruct
is a large type with a huuugeimpl
block and lots ofunsafe
code.Watching field accesses is vital to maintain memory safety in this type, so
pub
fields are generally unwanted.Could be written as:
The lint prevented potential changes that could affect memory safety. For example,
another_field
could be a computed offset that shouldn't be affected.The text was updated successfully, but these errors were encountered: