mirror of
https://github.com/github/codeql.git
synced 2026-04-30 03:05:15 +02:00
C#: Remove duplicated pattern extraction logic
This commit is contained in:
@@ -50,6 +50,8 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
}
|
||||
|
||||
throw new InternalError(varPattern, "Unable to get the declared symbol of the var pattern designation.");
|
||||
case DiscardDesignationSyntax discard:
|
||||
return new Expressions.Discard(cx, discard, parent, child);
|
||||
default:
|
||||
throw new InternalError("var pattern designation is unhandled");
|
||||
}
|
||||
|
||||
@@ -68,53 +68,9 @@ namespace Semmle.Extraction.CSharp.Entities.Statements
|
||||
private CasePattern(Context cx, CasePatternSwitchLabelSyntax node, Switch parent, int child)
|
||||
: base(cx, node, parent, child) { }
|
||||
|
||||
private void PopulatePattern(PatternSyntax pattern, TypeSyntax optionalType, VariableDesignationSyntax designation)
|
||||
{
|
||||
var isVar = optionalType is null;
|
||||
switch (designation)
|
||||
{
|
||||
case SingleVariableDesignationSyntax _:
|
||||
if (cx.GetModel(pattern).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
|
||||
{
|
||||
var type = Type.Create(cx, symbol.GetAnnotatedType());
|
||||
Expressions.VariableDeclaration.Create(cx, symbol, type, optionalType, cx.Create(pattern.GetLocation()), isVar, this, 0);
|
||||
}
|
||||
break;
|
||||
case DiscardDesignationSyntax discard:
|
||||
if (isVar)
|
||||
new Expressions.Discard(cx, discard, this, 0);
|
||||
else
|
||||
Expressions.TypeAccess.Create(cx, optionalType, this, 0);
|
||||
break;
|
||||
case null:
|
||||
break;
|
||||
case ParenthesizedVariableDesignationSyntax paren:
|
||||
Expressions.VariableDeclaration.CreateParenthesized(cx, (VarPatternSyntax)pattern, paren, this, 0);
|
||||
break;
|
||||
default:
|
||||
throw new InternalError(pattern, "Unhandled designation in case statement");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void PopulateStatement(TextWriter trapFile)
|
||||
{
|
||||
switch (Stmt.Pattern)
|
||||
{
|
||||
case VarPatternSyntax varPattern:
|
||||
PopulatePattern(varPattern, null, varPattern.Designation);
|
||||
break;
|
||||
case DeclarationPatternSyntax declarationPattern:
|
||||
PopulatePattern(declarationPattern, declarationPattern.Type, declarationPattern.Designation);
|
||||
break;
|
||||
case ConstantPatternSyntax pattern:
|
||||
Expression.Create(cx, pattern.Expression, this, 0);
|
||||
break;
|
||||
case RecursivePatternSyntax recPattern:
|
||||
new Expressions.RecursivePattern(cx, recPattern, this, 0);
|
||||
break;
|
||||
default:
|
||||
throw new InternalError(Stmt, "Case pattern not handled");
|
||||
}
|
||||
Expressions.Pattern.Create(cx, Stmt.Pattern, this, 0);
|
||||
|
||||
if (Stmt.WhenClause != null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user