about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorCatherine Flores <catherine.3.flores@gmail.com>2025-03-05 22:38:15 +0000
committerGitHub <noreply@github.com>2025-03-05 22:38:15 +0000
commit231bf457bac4eaba6ee183d8d513f824944b4476 (patch)
tree5e5ebf5e44894b3f89c770fcfcb1b90b874eeced /tests
parent06e75900f7ef061360ee8c80d8064b8b06ffb712 (diff)
parent8c73b76fb2f32674f76254e07474a596afee8120 (diff)
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
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/struct_fields.rs27
-rw-r--r--tests/ui/struct_fields.stderr21
2 files changed, 47 insertions, 1 deletions
diff --git a/tests/ui/struct_fields.rs b/tests/ui/struct_fields.rs
index 3dce530efff..e7ff2e6573b 100644
--- a/tests/ui/struct_fields.rs
+++ b/tests/ui/struct_fields.rs
@@ -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() {}
diff --git a/tests/ui/struct_fields.stderr b/tests/ui/struct_fields.stderr
index 79186cc1cfd..a5ff1b12590 100644
--- a/tests/ui/struct_fields.stderr
+++ b/tests/ui/struct_fields.stderr
@@ -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