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.