diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_explicit_cast.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_explicit_cast.dart index e9df7e2acc0..d2796d4b4d6 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/add_explicit_cast.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/add_explicit_cast.dart @@ -10,6 +10,7 @@ import 'package:analyzer/dart/ast/precedence.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/src/dart/ast/extensions.dart'; import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; +import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart'; import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; import 'package:analyzer_plugin/utilities/range_factory.dart'; @@ -84,12 +85,16 @@ class AddExplicitCast extends CorrectionProducer { toType.isDartCoreList) || (fromType.isDartCoreIterable || fromType.isDartCoreSet) && toType.isDartCoreSet)) { + final toType_final = toType; if (target.isCastMethodInvocation) { - // TODO(brianwilkerson) Consider updating the type arguments to the - // `cast` invocation. + var typeArguments = (target as MethodInvocation).typeArguments; + if (typeArguments != null) { + await builder.addDartFileEdit(file, (builder) { + _replaceTypeArgument(builder, typeArguments, toType_final, 0); + }); + } return; } - final toType_final = toType; await builder.addDartFileEdit(file, (builder) { if (needsParentheses) { builder.addSimpleInsertion(target_final.offset, '('); @@ -106,12 +111,17 @@ class AddExplicitCast extends CorrectionProducer { } else if (fromType.isDartCoreMap && toType is InterfaceType && toType.isDartCoreMap) { + final toType_final = toType; if (target.isCastMethodInvocation) { - // TODO(brianwilkerson) Consider updating the type arguments to the - // `cast` invocation. + var typeArguments = (target as MethodInvocation).typeArguments; + if (typeArguments != null) { + await builder.addDartFileEdit(file, (builder) { + _replaceTypeArgument(builder, typeArguments, toType_final, 0); + _replaceTypeArgument(builder, typeArguments, toType_final, 1); + }); + } return; } - final toType_final = toType; await builder.addDartFileEdit(file, (builder) { if (needsParentheses) { builder.addSimpleInsertion(target_final.offset, '('); @@ -142,4 +152,14 @@ class AddExplicitCast extends CorrectionProducer { }); } } + + /// Replace the type argument of [typeArguments] at the specified [index] + /// with the corresponding type argument of [toType]. + void _replaceTypeArgument(DartFileEditBuilder builder, + TypeArgumentList typeArguments, InterfaceType toType, int index) { + var replacementRange = range.node(typeArguments.arguments[index]); + builder.addReplacement(replacementRange, (builder) { + builder.writeType(toType.typeArguments[index]); + }); + } } diff --git a/pkg/analysis_server/test/src/services/correction/fix/add_explicit_cast_test.dart b/pkg/analysis_server/test/src/services/correction/fix/add_explicit_cast_test.dart index a8a06d46006..7033b5ae34a 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/add_explicit_cast_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/add_explicit_cast_test.dart @@ -105,7 +105,28 @@ class B {} '''); } - Future test_assignment_iterable_to_set() async { + Future test_assignment_iterable_cast() async { + await resolveTestCode(''' +f(Set a) { + Set b; + b = a.cast(); + print(b); +} +class A {} +class B {} +'''); + await assertHasFix(''' +f(Set a) { + Set b; + b = a.cast(); + print(b); +} +class A {} +class B {} +'''); + } + + Future test_assignment_iterable_toSet() async { await resolveTestCode(''' f(List a) { Set b; @@ -212,6 +233,27 @@ class B {} '''); } + Future test_assignment_map_cast() async { + await resolveTestCode(''' +f(Map a) { + Map b; + b = a.cast(); + print(b); +} +class A {} +class B {} +'''); + await assertHasFix(''' +f(Map a) { + Map b; + b = a.cast(); + print(b); +} +class A {} +class B {} +'''); + } + Future test_assignment_needsParens() async { await resolveTestCode(''' f(A a) {