From c84ada0fd454d70703f81f66d6c957882c61fafe Mon Sep 17 00:00:00 2001 From: pq Date: Wed, 11 Nov 2020 21:08:29 +0000 Subject: [PATCH] be aware of cascades when scraping type params for `var` replacement Fixes: https://github.com/dart-lang/sdk/issues/44156 Change-Id: I5ceb69d4a3fa4bbc68cdb7af2f9eb065ac8ae0f4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/171627 Reviewed-by: Brian Wilkerson Commit-Queue: Phil Quitslund --- .../correction/dart/replace_with_var.dart | 3 ++ .../correction/fix/replace_with_var_test.dart | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.dart index cd226a86b8c..8ac0b18d587 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.dart @@ -42,6 +42,9 @@ class ReplaceWithVar extends CorrectionProducer { String typeArgumentsText; int typeArgumentsOffset; if (type is NamedType && type.typeArguments != null) { + if (initializer is CascadeExpression) { + initializer = (initializer as CascadeExpression).target; + } if (initializer is TypedLiteral) { if (initializer.typeArguments == null) { typeArgumentsText = utils.getNodeText(type.typeArguments); diff --git a/pkg/analysis_server/test/src/services/correction/fix/replace_with_var_test.dart b/pkg/analysis_server/test/src/services/correction/fix/replace_with_var_test.dart index d06987688c0..422034efa07 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/replace_with_var_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/replace_with_var_test.dart @@ -74,6 +74,21 @@ void f(List list) { '''); } + Future test_generic_instanceCreation_cascade() async { + await resolveTestCode(''' +Set f() { + Set s = Set()..addAll([]); + return s; +} +'''); + await assertHasFix(''' +Set f() { + var s = Set()..addAll([]); + return s; +} +'''); + } + Future test_generic_instanceCreation_withArguments() async { await resolveTestCode(''' C f() { @@ -193,6 +208,21 @@ Set f() { await assertNoFix(); } + Future test_generic_setLiteral_cascade() async { + await resolveTestCode(''' +Set f() { + Set s = {}..addAll([]); + return s; +} +'''); + await assertHasFix(''' +Set f() { + var s = {}..addAll([]); + return s; +} +'''); + } + Future test_generic_setLiteral_const() async { await resolveTestCode(''' String f() {