Skip to content

Commit 57c3b57

Browse files
committed
C#: Narrow types to SingleVariableDesignation syntax (to avoid future compiler warning).
1 parent c6d2bf2 commit 57c3b57

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/Pattern.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ internal static Expression Create(Context cx, PatternSyntax syntax, IExpressionP
2828
case DeclarationPatternSyntax declPattern:
2929
// Creates a single local variable declaration.
3030
{
31-
if (declPattern.Designation is VariableDesignationSyntax designation)
31+
switch (declPattern.Designation)
3232
{
33-
if (cx.GetModel(syntax).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
34-
{
35-
var type = symbol.GetAnnotatedType();
36-
return VariableDeclaration.Create(cx, symbol, type, declPattern.Type, cx.CreateLocation(syntax.GetLocation()), false, parent, child);
37-
}
38-
if (designation is DiscardDesignationSyntax)
39-
{
33+
case SingleVariableDesignationSyntax singleDesignation:
34+
if (cx.GetModel(syntax).GetDeclaredSymbol(singleDesignation) is ILocalSymbol symbol)
35+
{
36+
var type = symbol.GetAnnotatedType();
37+
return VariableDeclaration.Create(cx, symbol, type, declPattern.Type, cx.CreateLocation(syntax.GetLocation()), false, parent, child);
38+
}
39+
throw new InternalError(singleDesignation, "Unable to get the declared symbol of the declaration pattern designation.");
40+
case DiscardDesignationSyntax _:
4041
return Expressions.TypeAccess.Create(cx, declPattern.Type, parent, child);
41-
}
42-
throw new InternalError(designation, "Designation pattern not handled");
42+
default:
43+
throw new InternalError($"declaration pattern designation of type {declPattern.Designation.GetType()} is unhandled");
4344
}
44-
throw new InternalError(declPattern, "Declaration pattern not handled");
4545
}
4646

4747
case RecursivePatternSyntax recPattern:
@@ -59,7 +59,6 @@ internal static Expression Create(Context cx, PatternSyntax syntax, IExpressionP
5959
if (cx.GetModel(syntax).GetDeclaredSymbol(varDesignation) is ILocalSymbol symbol)
6060
{
6161
var type = symbol.GetAnnotatedType();
62-
6362
return VariableDeclaration.Create(cx, symbol, type, null, cx.CreateLocation(syntax.GetLocation()), true, parent, child);
6463
}
6564

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RecursivePattern.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public RecursivePattern(Context cx, RecursivePatternSyntax syntax, IExpressionPa
2222
Expressions.TypeAccess.Create(cx, t, this, 1);
2323

2424
// Extract the local variable declaration
25-
if (syntax.Designation is VariableDesignationSyntax designation && cx.GetModel(syntax).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
25+
if (syntax.Designation is SingleVariableDesignationSyntax designation && cx.GetModel(syntax).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
2626
{
2727
var type = symbol.GetAnnotatedType();
2828

0 commit comments

Comments
 (0)