Skip to content

[analysis_server] Offer "create constructor" fix for non-final uninitialized fields - #64032

Open
hkarmoush wants to merge 2 commits into
dart-lang:mainfrom
hkarmoush:create-constructor-fix-nonfinal-fields
Open

[analysis_server] Offer "create constructor" fix for non-final uninitialized fields#64032
hkarmoush wants to merge 2 commits into
dart-lang:mainfrom
hkarmoush:create-constructor-fix-nonfinal-fields

Conversation

@hkarmoush

Copy link
Copy Markdown

Fixes #47389

If you write int a; with no initializer, analyzer only offers to fix it with "Add late". But if you write final int a; instead, you also get a "create a constructor for me" quick-fix — that option just never got extended to non-final fields, even though nothing about the underlying logic actually required final.

This reuses the same constructor-generating producer for the non-final case. As a bonus, it also handles classes that mix final and non-final uninitialized fields in one shot — previously you'd only get a fix that covered the final ones.

Two things needed guarding since non-final fields behave differently from final ones:

  • A nullable non-final field (int? b;) is already valid Dart without a constructor, so those are left alone.
  • Flutter widget constructors are generated as const, but const requires every field to be final — that gets dropped automatically when a non-final field is involved.

Test plan

  • Added 7 new test cases to create_constructor_for_final_fields_test.dart covering: a single non-final field, mixed final/non-final fields, private fields, late-field exclusion, nullable-non-final fields being left untouched, and the Flutter widget case (confirming const is correctly dropped).
  • Full pkg/analysis_server/test/src/services/correction/fix/ suite (4762 tests) passes with no regressions.
  • dart format / dart analyze clean.

…ialized fields

Non-nullable instance fields only ever got an "Add late" quick-fix,
even though the same "create a constructor" fix already existed for
their final-field sibling. Reusing that producer for non-final fields
too closes the gap and, as a bonus, handles classes that mix final and
non-final uninitialized fields in one shot instead of offering a fix
that only covers half the class.

The non-final case needed two guards the final-only version didn't:
skip nullable fields (they're already valid without a constructor) and
drop `const` from the generated Flutter widget constructor when a
non-final field is involved, since `const` requires every field to be
final.

Fixes dart-lang#47389
@copybara-service

Copy link
Copy Markdown

Thank you for your contribution! This project uses Gerrit for code reviews. Your pull request has automatically been converted into a code review at:

https://dart-review.googlesource.com/c/sdk/+/534805

Please wait for a developer to review your code review at the above link; you can speed up the review if you sign into Gerrit and manually add a reviewer that has recently worked on the relevant code. See CONTRIBUTING.md to learn how to upload changes to Gerrit directly.

Additional commits pushed to this PR will update both the PR and the corresponding Gerrit CL. After the review is complete on the CL, your reviewer will merge the CL (automatically closing this PR).

…d fields for const eligibility

canBeConst only inspected the class's own field declarations, so a
class with an inherited or mixed-in non-final field (making it
ineligible for a const constructor) still got one generated for it.
ClassElement.hasNonFinalField already walks the supertype and mixin
chain, so use it instead when a ClassElement is available.

Enum containers don't have this problem to begin with (enum instance
fields must always be final), so canBeConst keeps the old check for
that case.
@bwilkerson

Copy link
Copy Markdown
Member

It appears that copybara can't copy your latest changes to Gerrit because of a CLA related issue. Let us know if you need help resolving the issue.

@mraleph

mraleph commented Aug 20, 2026

Copy link
Copy Markdown
Member

I flushed CLA check by forcing a rescan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[analyzer] add a fix for a generative constructor for NOT_INITIALIZED_NON_NULLABLE_INSTANCE_FIELD

3 participants