Skip to content

Commit d29a6ec

Browse files
authored
Merge pull request github#4654 from tamasvajk/feature/csharp9-parens-pattern
C#: Handle parenthesized pattern extraction
2 parents 6fecc38 + 9b8d977 commit d29a6ec

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ internal static Expression Create(Context cx, PatternSyntax syntax, IExpressionP
1111
{
1212
switch (syntax)
1313
{
14+
case ParenthesizedPatternSyntax parenthesizedPattern:
15+
return Pattern.Create(cx, parenthesizedPattern.Pattern, parent, child);
16+
1417
case ConstantPatternSyntax constantPattern:
1518
return Expression.Create(cx, constantPattern.Expression, parent, child);
1619

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
3+
class T { }
4+
5+
class ParenthesizedPattern
6+
{
7+
void M1(object o)
8+
{
9+
if (o is {} p1)
10+
{
11+
}
12+
13+
if (o is ({} p2))
14+
{
15+
}
16+
}
17+
18+
19+
void M2(object o)
20+
{
21+
var r = o switch
22+
{
23+
1 => 1,
24+
(2) => 2,
25+
T t when t is {} => 3,
26+
(object o1) when o1 is ({}) => 4,
27+
(string _) => 5
28+
};
29+
}
30+
}

csharp/ql/test/library-tests/csharp9/PrintAst.expected

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,67 @@ NativeInt.cs:
184184
# 25| 0: [CastExpr] (...) ...
185185
# 25| 1: [LocalVariableAccess] access to local variable x
186186
# 25| 1: [LocalVariableAccess] access to local variable v
187+
ParenthesizedPattern.cs:
188+
# 3| [Class] T
189+
# 5| [Class] ParenthesizedPattern
190+
# 7| 5: [Method] M1
191+
# 7| -1: [TypeMention] Void
192+
#-----| 2: (Parameters)
193+
# 7| 0: [Parameter] o
194+
# 7| -1: [TypeMention] object
195+
# 8| 4: [BlockStmt] {...}
196+
# 9| 0: [IfStmt] if (...) ...
197+
# 9| 0: [IsExpr] ... is ...
198+
# 9| 0: [ParameterAccess] access to parameter o
199+
# 9| 1: [RecursivePatternExpr] { ... }
200+
# 9| 0: [LocalVariableDeclExpr] Object p1
201+
# 9| 3: [PropertyPatternExpr] { ... }
202+
# 10| 1: [BlockStmt] {...}
203+
# 13| 1: [IfStmt] if (...) ...
204+
# 13| 0: [IsExpr] ... is ...
205+
# 13| 0: [ParameterAccess] access to parameter o
206+
# 13| 1: [RecursivePatternExpr] { ... }
207+
# 13| 0: [LocalVariableDeclExpr] Object p2
208+
# 13| 3: [PropertyPatternExpr] { ... }
209+
# 14| 1: [BlockStmt] {...}
210+
# 19| 6: [Method] M2
211+
# 19| -1: [TypeMention] Void
212+
#-----| 2: (Parameters)
213+
# 19| 0: [Parameter] o
214+
# 19| -1: [TypeMention] object
215+
# 20| 4: [BlockStmt] {...}
216+
# 21| 0: [LocalVariableDeclStmt] ... ...;
217+
# 21| 0: [LocalVariableDeclAndInitExpr] Int32 r = ...
218+
# 21| -1: [TypeMention] int
219+
# 21| 0: [LocalVariableAccess] access to local variable r
220+
# 21| 1: [SwitchExpr] ... switch { ... }
221+
# 21| -1: [ParameterAccess] access to parameter o
222+
# 23| 0: [SwitchCaseExpr] ... => ...
223+
# 23| 0: [ConstantPatternExpr,IntLiteral] 1
224+
# 23| 2: [IntLiteral] 1
225+
# 24| 1: [SwitchCaseExpr] ... => ...
226+
# 24| 0: [ConstantPatternExpr,IntLiteral] 2
227+
# 24| 2: [IntLiteral] 2
228+
# 25| 2: [SwitchCaseExpr] ... => ...
229+
# 25| 0: [VariablePatternExpr] T t
230+
# 25| 0: [TypeMention] T
231+
# 25| 1: [IsExpr] ... is ...
232+
# 25| 0: [LocalVariableAccess] access to local variable t
233+
# 25| 1: [RecursivePatternExpr] { ... }
234+
# 25| 3: [PropertyPatternExpr] { ... }
235+
# 25| 2: [IntLiteral] 3
236+
# 26| 3: [SwitchCaseExpr] ... => ...
237+
# 26| 0: [VariablePatternExpr] Object o1
238+
# 26| 0: [TypeMention] object
239+
# 26| 1: [IsExpr] ... is ...
240+
# 26| 0: [LocalVariableAccess] access to local variable o1
241+
# 26| 1: [RecursivePatternExpr] { ... }
242+
# 26| 3: [PropertyPatternExpr] { ... }
243+
# 26| 2: [IntLiteral] 4
244+
# 27| 4: [SwitchCaseExpr] ... => ...
245+
# 27| 0: [TypeAccessPatternExpr] access to type String
246+
# 27| 0: [TypeMention] string
247+
# 27| 2: [IntLiteral] 5
187248
TypeParameterNullability.cs:
188249
# 1| [Interface] I1
189250
# 3| [Class] A2

0 commit comments

Comments
 (0)