-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Consider silencing struct_field_names
for structs with derive(Serialize/Deserialize)
#12035
Comments
@rustbot label +T-macros |
I'll try looking into it. |
I've done some test and it looks like the It looks logical to me but is it really the wanted behavior? On the other hand, it seems that |
The issue is that this is triggered for structs that derive Serialize and Deserialize, which are likely part of a public API. So, regardless of the number of fields; if these traits are impl'd or derived, then the lint shouldn't trigger on the basis of those structures representing data across programs. I think that is reasonable, but maybe it is not desirable? What does everyone think? @rustbot label +S-needs-discussion |
Sorry, I didn't make myself really clear... #![warn(clippy::pedantic)]
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Resource {
pub(crate) id: String,
pub(crate) name: String,
pub(crate) resource_group: String,
} But this will not #![warn(clippy::pedantic)]
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Resource {
pub id: String,
pub name: String,
pub resource_group: String,
} The fact is that it's not silenced because it's only public in the crate scope, so it will never be used in other crates or programs |
Visibility shouldn't matter because the struct can be private and at the same time be used to deserialize data sent from across the network. |
Ho ok ! I see what you mean |
It might be fine short-term to allow specifically That way we wouldn't need to check for specific derives, because this FP doesn't affect just Worth noting (haven't seen anyone point this out) that this also affects the enum_variant_names lint |
I think a tag is a better approach and makes it agnostic to libraries. |
Looks good to me. We want a #![warn(clippy::pedantic)]
#[clippy::significant_field_names]
struct A {
a_id: String,
a_name: String,
a_label: String,
} or something like that #![warn(clippy::pedantic)]
#[clippy::significant_field_names]
enum Ab {
IdAb,
NameAb,
LabelAb,
} Should not trigger any warning. This |
I don't think derive macros can return attributes or generally modify the annotated item. If that was possible, macro authors could already place Now that I think about it, maybe we don't need a new attribute and just You could also ask on zulip to get more opinions if you want to be sure before committing to it (or if you have any questions you can also ask there), it's likely more people will see it there |
I'm not sure I agree with the core rationale that Serialize/Deserialize are always "public", it depends on what kind of format is expected to be used with them. But I agree in general it doesn't hurt to allow impls to turn off this lint. |
…names, r=Manishearth Fix/12035 silence struct field names fixes #12035 Allows to silence lints `struct_field_names` and `enum_variant_names` through their impls. This is done in order for some crates such as `serde` to silence those lints changelog: [`struct_field_names`]: allow to silence lint through struct's impl changelog: [`enum_variant_names`]: allow to silence lint through enum's impl
…names, r=Manishearth Fix/12035 silence struct field names fixes #12035 Allows to silence lints `struct_field_names` and `enum_variant_names` through their impls. This is done in order for some crates such as `serde` to silence those lints changelog: [`struct_field_names`]: allow to silence lint through struct's impl changelog: [`enum_variant_names`]: allow to silence lint through enum's impl
In the latest meeting we decided, that the responsibility of adding this |
Summary
Clippy silences
struct_field_names
for public structs. This is good and makes sense; the field is part of the public API and renaming it would be a breaking change. However, it still emits the lint for structs withderive(Deserialize)
. I think these should also be silenced, becauseSerialize
andDeserialize
are "super public" - not just available to other crates, but to other programs as well.Lint Name
struct_field_names
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen: No lint is emitted.
As an aside, the lint stops firing if i remove
id
orname
, which seems quite odd.Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: