C#: Fix extracted type for nested object initializers

This commit is contained in:
Tom Hvitved
2020-05-20 14:16:00 +02:00
parent 70d47b76b1
commit 011a95dcfa
2 changed files with 18 additions and 9 deletions

View File

@@ -69,9 +69,18 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
if (assignment != null)
{
var assignmentEntity = new Expression(new ExpressionNodeInfo(cx, init, this, child++).SetKind(ExprKind.SIMPLE_ASSIGN));
CreateFromNode(new ExpressionNodeInfo(cx, assignment.Right, assignmentEntity, 0));
var assignmentInfo = new ExpressionNodeInfo(cx, init, this, child++).SetKind(ExprKind.SIMPLE_ASSIGN);
var assignmentEntity = new Expression(assignmentInfo);
var typeInfoRight = cx.GetTypeInfo(assignment.Right);
if (typeInfoRight.Type is null)
// The type may be null for nested initializers such as
// ```
// new ClassWithArrayField() { As = { [0] = a } }
// ```
// In this case we take the type from the assignment
// `As = { [0] = a }` instead
typeInfoRight = assignmentInfo.TypeInfo;
CreateFromNode(new ExpressionNodeInfo(cx, assignment.Right, assignmentEntity, 0, typeInfoRight));
var target = cx.GetSymbolInfo(assignment.Left);

View File

@@ -79,9 +79,9 @@ initializers
initializerType
| csharp6.cs:68:50:68:91 | { ..., ... } | Dictionary<Int32, String> |
| csharp6.cs:72:9:79:9 | { ..., ... } | Compound |
| csharp6.cs:73:31:73:72 | { ..., ... } | null |
| csharp6.cs:74:34:74:76 | { ..., ... } | null |
| csharp6.cs:75:26:75:54 | { ..., ... } | null |
| csharp6.cs:76:27:76:56 | { ..., ... } | null |
| csharp6.cs:77:29:77:56 | { ..., ... } | null |
| csharp6.cs:78:30:78:59 | { ..., ... } | null |
| csharp6.cs:73:31:73:72 | { ..., ... } | Dictionary<Int32, String> |
| csharp6.cs:74:34:74:76 | { ..., ... } | Dictionary<Int32, String> |
| csharp6.cs:75:26:75:54 | { ..., ... } | String[] |
| csharp6.cs:76:27:76:56 | { ..., ... } | String[,] |
| csharp6.cs:77:29:77:56 | { ..., ... } | String[] |
| csharp6.cs:78:30:78:59 | { ..., ... } | String[,] |