diff --git a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs index b90970ce6a831affd5dbc25e7e41ee468c3e944f..81f24808024c9d6f976c3c21d0319970b6692ee8 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs @@ -5132,6 +5132,46 @@ void M() await TestInRegularAndScriptAsync(code, expected, index: 1); } + [WorkItem(31795, "https://github.com/dotnet/roslyn/issues/31795")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] + public async Task TestInAnonymousObjectMemberDeclaratorWithInferredType() + { + var code = +@"using System; +using System.Collections.Generic; +using System.Linq; + +class C +{ + void Test(Dictionary> d) + { + _ = new + { + a = [|d.Values|].Where(l => l.Count == 1) + }; + } +}"; + + var expected = +@"using System; +using System.Collections.Generic; +using System.Linq; + +class C +{ + void Test(Dictionary> d) + { + Dictionary>.ValueCollection {|Rename:values|} = d.Values; + _ = new + { + a = values.Where(l => l.Count == 1) + }; + } +}"; + + await TestInRegularAndScriptAsync(code, expected); + } + [WorkItem(2423, "https://github.com/dotnet/roslyn/issues/2423")] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] public async Task TestPickNameBasedOnArgument1() diff --git a/src/Workspaces/CSharp/Portable/Utilities/SpeculationAnalyzer.cs b/src/Workspaces/CSharp/Portable/Utilities/SpeculationAnalyzer.cs index e4e6afdfc6756da7b21b3276130d2210ec3162e7..eeb9559a1d2e7534cd8ac337095eaa5a3145cf44 100644 --- a/src/Workspaces/CSharp/Portable/Utilities/SpeculationAnalyzer.cs +++ b/src/Workspaces/CSharp/Portable/Utilities/SpeculationAnalyzer.cs @@ -491,7 +491,7 @@ private bool ReplacementBreaksAnonymousObjectMemberDeclarator(AnonymousObjectMem { var originalExpressionType = this.OriginalSemanticModel.GetTypeInfo(originalAnonymousObjectMemberDeclarator.Expression, this.CancellationToken).Type; var newExpressionType = this.SpeculativeSemanticModel.GetTypeInfo(replacedAnonymousObjectMemberDeclarator.Expression, this.CancellationToken).Type; - return originalExpressionType != newExpressionType; + return !originalExpressionType.Equals(newExpressionType); } private bool ReplacementBreaksConstructorInitializer(ConstructorInitializerSyntax ctorInitializer, ConstructorInitializerSyntax newCtorInitializer)