From e171e636d508b87b238751dc4267f9368a72f61d Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Thu, 11 Jun 2026 06:51:41 -0700 Subject: [PATCH] Do not offer to bind a declaring parameter to a field Closes https://github.com/dart-lang/sdk/issues/63540 Change-Id: I72f8f76878e5a058f58aca80b1543a1141a68b72 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/511100 Commit-Queue: Brian Wilkerson Reviewed-by: Samuel Rawlins --- .../lib/src/services/correction/dart/bind_to_field.dart | 7 +++++++ .../src/services/correction/assist/bind_to_field_test.dart | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/bind_to_field.dart b/pkg/analysis_server/lib/src/services/correction/dart/bind_to_field.dart index e95ac2790ef..23cc35b0ba4 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/bind_to_field.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/bind_to_field.dart @@ -44,6 +44,13 @@ class BindToField extends ResolvedCorrectionProducer { return; } } + if (parameter.declaredFragment?.element + case FieldFormalParameterElement element) { + if (element.isDeclaring) { + // A declaring parameter is already bound to a field. + return; + } + } await tryReplacingParameter(file, builder, parameter, libraryElement2); } } diff --git a/pkg/analysis_server/test/src/services/correction/assist/bind_to_field_test.dart b/pkg/analysis_server/test/src/services/correction/assist/bind_to_field_test.dart index 2faad80ac69..1d88919c0b8 100644 --- a/pkg/analysis_server/test/src/services/correction/assist/bind_to_field_test.dart +++ b/pkg/analysis_server/test/src/services/correction/assist/bind_to_field_test.dart @@ -21,6 +21,13 @@ class BindToFieldTest extends AssistProcessorTest { @override AssistKind get kind => DartAssistKind.bindToField; + Future test_class_primaryConstructor_declaring() async { + await resolveTestCode(''' +class C(final int ^i); +'''); + await assertNoAssist(); + } + Future test_class_primaryConstructor_requiredPositional() async { await resolveTestCode(''' class C(int ^i);