From bf5bdc97ffca0bcaa0f79fc299ddcf1079ffe307 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Mon, 8 May 2023 14:54:48 +0000 Subject: [PATCH] Issue 52296. Fix NPE in AddTypeAnnotation, when formal parameter without name. Bug: https://github.com/dart-lang/sdk/issues/52296 Change-Id: I6f3952faa9f3fc9e7f9e1d83c4699222be8dbcdc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/301731 Reviewed-by: Brian Wilkerson Reviewed-by: Keerti Parthasarathy Commit-Queue: Konstantin Shcheglov --- .../correction/dart/add_type_annotation.dart | 13 ++++++++++--- .../correction/assist/add_type_annotation_test.dart | 8 ++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart index 0ff5f7956db..151400065a0 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart @@ -47,10 +47,12 @@ class AddTypeAnnotation extends CorrectionProducer { @override Future compute(ChangeBuilder builder) async { final node = this.node; + if (node is SimpleFormalParameter) { - await _forSimpleFormalParameter(builder, node.name!, node); + await _forSimpleFormalParameter(builder, node); return; } + if (node is DeclaredVariablePattern) { var type = node.matchedValueType; var keyword = node.keyword; @@ -133,12 +135,17 @@ class AddTypeAnnotation extends CorrectionProducer { builder, declaredIdentifier.keyword, declaredIdentifier.name, type); } - Future _forSimpleFormalParameter(ChangeBuilder builder, Token name, - SimpleFormalParameter parameter) async { + Future _forSimpleFormalParameter( + ChangeBuilder builder, SimpleFormalParameter parameter) async { // Ensure that there isn't already a type annotation. if (parameter.type != null) { return; } + // Ensure that the parameter is named. + final name = parameter.name; + if (name == null) { + return; + } // Prepare the type. var type = parameter.declaredElement!.type; // TODO(scheglov) If the parameter is in a method declaration, and if the diff --git a/pkg/analysis_server/test/src/services/correction/assist/add_type_annotation_test.dart b/pkg/analysis_server/test/src/services/correction/assist/add_type_annotation_test.dart index a57a14fc0fa..df67d9b5dd3 100644 --- a/pkg/analysis_server/test/src/services/correction/assist/add_type_annotation_test.dart +++ b/pkg/analysis_server/test/src/services/correction/assist/add_type_annotation_test.dart @@ -689,6 +689,14 @@ void f() { '''); } + Future test_parameter_final_type_noName() async { + verifyNoTestUnitErrors = false; + await resolveTestCode(''' +typedef F = void Function(final int); +'''); + await assertNoAssistAt('final'); + } + @FailingTest(reason: ''' This functionality is disabled in `AddTypeAnnotation._forSimpleFormalParameter` because `writeType` is writing the names of the parameters when it shouldn't.