Make struct_field_names check private fields of public structs. (#14076)

Currently, If a struct is `pub` and its field is private, and
`avoid-breaking-exported-api = true` (default), then
`struct_field_names` will not lint the field, even though changing the
field’s name is not a breaking change. This is because the
breaking-exported-api condition was checking the visibility of the
struct, not its fields (perhaps because the same code was used for
enums). With this change, Clippy will check the field’s effective
visibility only.

Note: This change is large because some functions were moved into an
`impl` to be able to access more configuration. Consider viewing the
diff with whitespace ignored.

changelog: [`struct_field_names`]: also check private fields of public
structs
This commit is contained in:
Catherine Flores 2025-03-05 22:38:15 +00:00 committed by GitHub
commit 231bf457ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 204 additions and 127 deletions

View file

@ -344,4 +344,31 @@ struct Use {
//~^ struct_field_names
}
// should lint on private fields of public structs (renaming them is not breaking-exported-api)
pub struct PubStructFieldNamedAfterStruct {
pub_struct_field_named_after_struct: bool,
//~^ ERROR: field name starts with the struct's name
other1: bool,
other2: bool,
}
pub struct PubStructFieldPrefix {
//~^ ERROR: all fields have the same prefix: `field`
field_foo: u8,
field_bar: u8,
field_baz: u8,
}
// ...but should not lint on structs with public fields.
pub struct PubStructPubAndPrivateFields {
/// One could argue that this field should be linted, but currently, any public field stops all
/// checking.
pub_struct_pub_and_private_fields_1: bool,
pub pub_struct_pub_and_private_fields_2: bool,
}
// nor on common prefixes if one of the involved fields is public
pub struct PubStructPubAndPrivateFieldPrefix {
pub field_foo: u8,
field_bar: u8,
field_baz: u8,
}
fn main() {}

View file

@ -281,5 +281,24 @@ error: field name starts with the struct's name
LL | use_baz: bool,
| ^^^^^^^^^^^^^
error: aborting due to 24 previous errors
error: field name starts with the struct's name
--> tests/ui/struct_fields.rs:349:5
|
LL | pub_struct_field_named_after_struct: bool,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: all fields have the same prefix: `field`
--> tests/ui/struct_fields.rs:354:1
|
LL | / pub struct PubStructFieldPrefix {
LL | |
LL | | field_foo: u8,
LL | | field_bar: u8,
LL | | field_baz: u8,
LL | | }
| |_^
|
= help: remove the prefixes
error: aborting due to 26 previous errors