From 4ecda6703ae1a2edc3ef5270852022491444b088 Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Fri, 17 Jan 2020 13:18:04 -0800 Subject: [PATCH] Handle newly added property sub-pattern operation in OperationExtensions.GetValueUsageInfo Fix #40499 --- .../RemoveUnusedValueAssignmentTests.cs | 54 +++++++++++++++++++ .../Extensions/OperationExtensions.cs | 8 +++ 2 files changed, 62 insertions(+) diff --git a/src/EditorFeatures/CSharpTest/RemoveUnusedParametersAndValues/RemoveUnusedValueAssignmentTests.cs b/src/EditorFeatures/CSharpTest/RemoveUnusedParametersAndValues/RemoveUnusedValueAssignmentTests.cs index b970e67e82e..8bb0ca1e8db 100644 --- a/src/EditorFeatures/CSharpTest/RemoveUnusedParametersAndValues/RemoveUnusedValueAssignmentTests.cs +++ b/src/EditorFeatures/CSharpTest/RemoveUnusedParametersAndValues/RemoveUnusedValueAssignmentTests.cs @@ -7415,6 +7415,60 @@ string M(object obj) }", new TestParameters(options: PreferUnusedLocal, parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8))); } + [WorkItem(40499, "https://github.com/dotnet/roslyn/issues/40499")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)] + public async Task LocalUsedWithPropertySubPattern() + { + await TestDiagnosticMissingAsync( +@"class C +{ + public object P { get; } + void M() + { + C [|c|] = new C(); + var x = c is { P : int i }; + } +}", new TestParameters(options: PreferDiscard, parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8))); + } + + [WorkItem(40499, "https://github.com/dotnet/roslyn/issues/40499")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)] + public async Task UnusedLocalDefinedInPropertySubPattern_PreferDiscard() + { + await TestInRegularAndScriptAsync( +@"class C +{ + public object P { get; } + void M(C c) + { + var x = c is { P : int [|i|] }; + } +}", +@"class C +{ + public object P { get; } + void M(C c) + { + var x = c is { P : int _ }; + } +}", options: PreferDiscard, parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8)); + } + + [WorkItem(40499, "https://github.com/dotnet/roslyn/issues/40499")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)] + public async Task UnusedLocalDefinedInPropertySubPattern_PreferUnusedLocal() + { + await TestDiagnosticMissingAsync( +@"class C +{ + public object P { get; } + void M(C c) + { + var x = c is { P : int [|i|] }; + } +}", new TestParameters(options: PreferUnusedLocal, parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8))); + } + [WorkItem(38640, "https://github.com/dotnet/roslyn/issues/38640")] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)] public async Task DeclarationPatternInSwitchExpressionArm_UnusedLocal_PreferDiscard() diff --git a/src/Workspaces/Core/Portable/Extensions/OperationExtensions.cs b/src/Workspaces/Core/Portable/Extensions/OperationExtensions.cs index 3e5adab1a5c..5ad882b2767 100644 --- a/src/Workspaces/Core/Portable/Extensions/OperationExtensions.cs +++ b/src/Workspaces/Core/Portable/Extensions/OperationExtensions.cs @@ -91,6 +91,14 @@ public static ValueUsageInfo GetValueUsageInfo(this IOperation operation) // return ValueUsageInfo.Write; + case IPropertySubpatternOperation _: + // A declaration pattern within a property sub-pattern is a + // write for the declared local. + // For example, 'x' is defined and assigned the value from 'obj.Property' below: + // if (obj is { Property : int x }) + // + return ValueUsageInfo.Write; + default: Debug.Fail("Unhandled declaration pattern context"); -- GitLab