Skip to content

Commit 98e125f

Browse files
committed
C#: Add library support for list- and slice patterns.
1 parent cfd3c1f commit 98e125f

File tree

1 file changed

+32
-0
lines changed
  • csharp/ql/lib/semmle/code/csharp/exprs

1 file changed

+32
-0
lines changed

csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ private predicate hasChildPattern(ControlFlowElement pm, Expr child) {
321321
mid instanceof @tuple_expr and
322322
child = mid.getAChildExpr()
323323
)
324+
or
325+
exists(Expr mid |
326+
hasChildPattern(pm, mid) and
327+
mid instanceof @list_pattern_expr and
328+
child = mid.getAChildExpr()
329+
)
330+
or
331+
exists(Expr mid |
332+
hasChildPattern(pm, mid) and
333+
mid instanceof @slice_pattern_expr and
334+
child = mid.getAChildExpr()
335+
)
324336
}
325337

326338
/**
@@ -506,6 +518,26 @@ class PositionalPatternExpr extends PatternExpr, @positional_pattern_expr {
506518
override string getAPrimaryQlClass() { result = "PositionalPatternExpr" }
507519
}
508520

521+
/** A list pattern. For example `[1, 2, int y]` in `x is [1, 2, int y]`. */
522+
class ListPatternExpr extends PatternExpr, @list_pattern_expr {
523+
override string toString() { result = "[ ... ]" }
524+
525+
/** Gets the `n`th pattern. */
526+
PatternExpr getPattern(int n) { result = this.getChild(n) }
527+
528+
override string getAPrimaryQlClass() { result = "ListPatternExpr" }
529+
}
530+
531+
/** A slice pattern. For example `..` in `x is [1, .., 2]. */
532+
class SlicePatternExpr extends PatternExpr, @slice_pattern_expr {
533+
override string toString() { result = ".." }
534+
535+
/** Gets the subpattern, if any. */
536+
PatternExpr getPattern() { result = this.getChild(0) }
537+
538+
override string getAPrimaryQlClass() { result = "SlicePatternExpr" }
539+
}
540+
509541
/** A unary pattern. For example, `not 1`. */
510542
class UnaryPatternExpr extends PatternExpr, @unary_pattern_expr {
511543
/** Gets the underlying pattern. */

0 commit comments

Comments
 (0)