Derive Default for structs when all field defaults are intrinsic#1035
Open
danieleades wants to merge 1 commit into
Open
Derive Default for structs when all field defaults are intrinsic#1035danieleades wants to merge 1 commit into
danieleades wants to merge 1 commit into
Conversation
When a struct has no whole-type default value and every property's default is Default::default(), the generated manual 'impl Default' is exactly what '#[derive(Default)]' produces, and trips clippy's warn-by-default 'derivable_impls' lint in consuming code (~50 warnings on the github.json test fixture alone). Add 'Default' to the derive set in that case instead of emitting the manual impl. Structs with a whole-type default value or any custom field default still get a hand-written impl, and builder-module Default impls are unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When every property of a struct has a default and none of them are custom, the generated code emits a hand-written
impl Defaultwhose every field initializer isDefault::default(). That impl is exactly what#[derive(Default)]produces, so clippy's warn-by-defaultderivable_implslint fires on it in consuming code — roughly 50 times on the output for a large schema like thegithub.jsontest fixture. It also bloats the generated output.Fix
In
output_struct, when there is no whole-type default value and every property's default is the intrinsicDefault::default(), addDefaultto the struct's derive set instead of emitting the manual impl.Hand-written impls are kept where they carry real information:
defaultvalue,Defaultimpls (which initialize fields toOk(..)/Err(..)).Trade-offs
None behavioral — the derived impl produces identical values to the removed manual one. The golden-file diff is large but mechanical: every removed impl had all-
Default::default()initializers, and its struct gainsDefaultin the derive list.