提交 58940158 编写于 作者: M Manish Vasani

Fix an assert in IOperationExtension.GetValueUsageInfo

Handle declaration pattern parented by switch expression arm in GetValueUsageInfo.
Fixes #38640
上级 49b6ee1a
......@@ -7175,6 +7175,69 @@ void M(List<(int, int)> list)
}");
}
[WorkItem(38640, "https://github.com/dotnet/roslyn/issues/38640")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
public async Task DeclarationPatternInSwitchExpressionArm_UsedLocal()
{
await TestDiagnosticMissingAsync(
@"class C
{
string M(object obj)
{
return obj switch
{
int [|p2|] => p2.ToString(),
_ => ""NoMatch""
};
}
}", new TestParameters(options: PreferDiscard, 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_PreferUnusedLocal()
{
await TestDiagnosticMissingAsync(
@"class C
{
string M(object obj)
{
return obj switch
{
int [|p2|] => ""Int"",
_ => ""NoMatch""
};
}
}", 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()
{
await TestInRegularAndScriptAsync(
@"class C
{
string M(object obj)
{
return obj switch
{
int [|p2|] => ""Int"",
_ => ""NoMatch""
};
}
}",
@"class C
{
string M(object obj)
{
return obj switch
{
int _ => ""Int"",
_ => ""NoMatch""
};
}
}", options: PreferDiscard, parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8));
}
}
}
......@@ -73,6 +73,16 @@ public static ValueUsageInfo GetValueUsageInfo(this IOperation operation)
//
return ValueUsageInfo.Write;
case ISwitchExpressionArmOperation _:
// A declaration pattern within a switch expression arm is a
// write for the declared local.
// For example, 'x' is defined and assigned the value from 'obj' below:
// obj switch
// {
// X x => ...
//
return ValueUsageInfo.Write;
case IIsPatternOperation _:
// A declaration pattern within an is pattern is a
// write for the declared local.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册