提交 dbb963eb 编写于 作者: N Neal Gafter 提交者: GitHub

Fix a NullReferenceException in DataFlowAnalysis.VariablesDeclared (#17375)

Fixes #17281
上级 72057f54
......@@ -77,7 +77,8 @@ private void NoteDeclaredPatternVariables(BoundPattern pattern)
if (IsInside && pattern.Kind == BoundKind.DeclarationPattern)
{
var decl = (BoundDeclarationPattern)pattern;
if (decl.Variable.Kind == SymbolKind.Local)
// The variable may be null if it is a discard designation `_`.
if (decl.Variable?.Kind == SymbolKind.Local)
{
// Because this API only returns local symbols and parameters,
// we exclude pattern variables that have become fields in scripts.
......
......@@ -4751,6 +4751,41 @@ public void F(ulong? p)
Assert.Equal("this, p", GetSymbolNamesJoined(dataFlowAnalysisResults.WrittenOutside));
}
[Fact]
[WorkItem(17281, "https://github.com/dotnet/roslyn/issues/17281")]
public void DiscardVsVariablesDeclared()
{
var dataFlowAnalysisResults = CompileAndAnalyzeDataFlowStatements(@"
class A { }
class Test
{
private void Repro(A node)
{
/*<bind>*/
switch (node)
{
case A _:
break;
case Unknown:
break;
default:
return;
}
/*</bind>*/
}
}");
Assert.Empty(dataFlowAnalysisResults.Captured);
Assert.Empty(dataFlowAnalysisResults.VariablesDeclared);
Assert.Empty(dataFlowAnalysisResults.AlwaysAssigned);
Assert.Equal("node", GetSymbolNamesJoined(dataFlowAnalysisResults.DataFlowsIn));
Assert.Empty(dataFlowAnalysisResults.DataFlowsOut);
Assert.Equal("node", GetSymbolNamesJoined(dataFlowAnalysisResults.ReadInside));
Assert.Equal(null, GetSymbolNamesJoined(dataFlowAnalysisResults.ReadOutside));
Assert.Empty(dataFlowAnalysisResults.WrittenInside);
Assert.Equal("this, node", GetSymbolNamesJoined(dataFlowAnalysisResults.WrittenOutside));
}
#endregion
#region "Misc."
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册