Skip to content

Commit 03e20ee

Browse files
authored
Merge pull request #4314 from tamasvajk/feature/switch-case-expr
C#: Fix switch case expression types
2 parents 5ab5e75 + 5f96c37 commit 03e20ee

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Switch.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ private Switch(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.SWITCH))
1818
protected override void PopulateExpression(TextWriter trapFile)
1919
{
2020
SwitchedExpr = Expression.Create(cx, Syntax.GoverningExpression, this, -1);
21-
int child = 0;
22-
foreach (var arm in Syntax.Arms)
21+
for (var i = 0; i < Syntax.Arms.Count; i++)
2322
{
24-
new SwitchCase(cx, arm, this, child++);
23+
new SwitchCase(cx, Syntax.Arms[i], this, i);
2524
}
2625
}
2726
}
2827

2928
class SwitchCase : Expression
3029
{
3130
internal SwitchCase(Context cx, SwitchExpressionArmSyntax arm, Switch parent, int child) :
32-
base(new ExpressionInfo(cx, parent.SwitchedExpr.Type, cx.Create(arm.GetLocation()), ExprKind.SWITCH_CASE, parent, child, false, null))
31+
base(new ExpressionInfo(
32+
cx, Entities.Type.Create(cx, cx.GetType(arm.Expression)), cx.Create(arm.GetLocation()),
33+
ExprKind.SWITCH_CASE, parent, child, false, null))
3334
{
3435
cx.CreatePattern(arm.Pattern, this, 0);
3536
if (arm.WhenClause is WhenClauseSyntax when)

csharp/ql/test/library-tests/controlflow/guards/Implications.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,14 @@
397397
| Guards.cs:276:16:276:16 | access to parameter o | match access to type Action<Object> | Guards.cs:276:16:276:16 | access to parameter o | non-null |
398398
| Guards.cs:276:16:276:16 | access to parameter o | match null | Guards.cs:276:16:276:16 | access to parameter o | null |
399399
| Guards.cs:276:16:276:16 | access to parameter o | non-match null | Guards.cs:276:16:276:16 | access to parameter o | non-null |
400+
| Guards.cs:278:13:279:28 | ... => ... | true | Guards.cs:276:16:276:16 | access to parameter o | non-null |
401+
| Guards.cs:280:13:281:28 | ... => ... | true | Guards.cs:276:16:276:16 | access to parameter o | non-null |
400402
| Guards.cs:281:17:281:17 | access to local variable a | non-null | Guards.cs:276:16:276:16 | access to parameter o | non-null |
401403
| Guards.cs:281:17:281:17 | access to local variable a | null | Guards.cs:276:16:276:16 | access to parameter o | null |
404+
| Guards.cs:282:13:283:28 | ... => ... | true | Guards.cs:276:16:276:16 | access to parameter o | non-null |
405+
| Guards.cs:284:13:285:28 | ... => ... | false | Guards.cs:276:16:276:16 | access to parameter o | non-null |
406+
| Guards.cs:284:13:285:28 | ... => ... | true | Guards.cs:276:16:276:16 | access to parameter o | null |
407+
| Guards.cs:286:13:287:28 | ... => ... | true | Guards.cs:276:16:276:16 | access to parameter o | non-null |
402408
| Guards.cs:296:16:296:17 | access to local variable b2 | match true | Guards.cs:294:13:294:14 | access to parameter b1 | false |
403409
| Guards.cs:296:16:296:17 | access to local variable b2 | match true | Guards.cs:296:16:296:17 | access to local variable b2 | true |
404410
| Guards.cs:308:16:308:17 | access to local variable b2 | match true | Guards.cs:306:13:306:14 | access to parameter b1 | true |
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
| patterns.cs:101:34:101:40 | "large" | String |
2+
| patterns.cs:102:18:102:24 | "small" | String |
3+
| patterns.cs:110:22:110:26 | (..., ...) | (Int32,Int32) |
4+
| patterns.cs:111:22:111:26 | (..., ...) | (Int32,Int32) |
5+
| patterns.cs:117:27:117:33 | (..., ...) | (Int32,Int32) |
6+
| patterns.cs:118:28:118:34 | (..., ...) | (Int32,Int32) |
7+
| patterns.cs:119:33:119:38 | (..., ...) | (Int32,Int32) |
8+
| patterns.cs:128:49:128:49 | 0 | Int32 |
9+
| patterns.cs:129:38:129:38 | 1 | Int32 |
10+
| patterns.cs:130:23:130:23 | 2 | Int32 |
11+
| patterns.cs:131:27:131:27 | 3 | Int32 |
12+
| patterns.cs:138:22:138:50 | throw ... | null |
13+
| patterns.cs:139:22:139:22 | 3 | Int32 |
14+
| patterns.cs:140:42:140:42 | 4 | Int32 |
15+
| patterns.cs:141:29:141:29 | 5 | Int32 |
16+
| patterns.cs:142:41:142:41 | 6 | Int32 |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import csharp
2+
3+
from SwitchCaseExpr case
4+
select case.getBody(), case.getType().toString()

0 commit comments

Comments
 (0)