Skip to content

Commit 4bff795

Browse files
committed
Fix record pattern and pretty-printing
1 parent 419d530 commit 4bff795

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

java/ql/lib/semmle/code/java/Expr.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,8 @@ class LocalVariableDeclExpr extends Expr, @localvariabledeclexpr {
16721672
exists(InstanceOfExpr ioe | this.getParent() = ioe | result.isNthChildOf(ioe, 1))
16731673
or
16741674
exists(PatternCase pc | this.getParent() = pc | result.isNthChildOf(pc, -2))
1675+
or
1676+
exists(RecordPatternExpr rpe, int index | this.isNthChildOf(rpe, index) and result.isNthChildOf(rpe, -(index + 1)) )
16751677
}
16761678

16771679
/** Gets the name of the variable declared by this local variable declaration expression. */

java/ql/lib/semmle/code/java/PrettyPrintAst.qll

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,12 +1078,30 @@ private class PpRecordPattern extends PpAst, RecordPatternExpr {
10781078
or
10791079
i = 1 and result = "("
10801080
or
1081-
i = 1 + ((any(int x | x >= 1 and exists(this.getSubPattern(x)))) * 2) and result = ", "
1081+
i = 1 + ((any(int x | x >= 1 and exists(this.getSubPattern(x)))) * 4) and result = ", "
10821082
or
1083-
i = 1 + (count(this.getSubPattern(_)) * 2) and result = ")"
1083+
i = 1 + (count(this.getSubPattern(_)) * 4) and result = ")"
1084+
or
1085+
exists(int x, LocalVariableDeclExpr v, int offset | v = this.getSubPattern(x) |
1086+
i = (offset) + (x * 4) and
1087+
(
1088+
offset = 2 and not exists(v.getTypeAccess()) and result = "var"
1089+
or
1090+
offset = 3 and result = " "
1091+
)
1092+
)
10841093
}
10851094

10861095
override PpAst getChild(int i) {
1087-
exists(int x | result = this.getSubPattern(x) | i = 2 + (x * 2))
1096+
exists(int x, PatternExpr subPattern, int offset | subPattern = this.getSubPattern(x) |
1097+
i = (offset) + (x * 4) and
1098+
(
1099+
result = subPattern.(RecordPatternExpr) and offset = 2
1100+
or
1101+
result = subPattern.(LocalVariableDeclExpr).getTypeAccess() and offset = 2
1102+
or
1103+
result = subPattern.(LocalVariableDeclExpr) and offset = 4
1104+
)
1105+
)
10881106
}
10891107
}

java/ql/test/library-tests/prettyprint/Test.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ case R(S(int x), String y):
3838
if (o instanceof String s) { }
3939
if (o instanceof R(S(int x), String y)) { }
4040

41+
switch(o) {
42+
case R(S(var x), var y) -> { }
43+
case null, default -> { }
44+
}
45+
46+
if (o instanceof R(S(var x), var y)) { }
47+
4148
}
4249

4350
}

java/ql/test/library-tests/prettyprint/pp.expected

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,43 @@
4646
| Test.java:1:14:1:17 | Test | 45 | switch (o) { |
4747
| Test.java:1:14:1:17 | Test | 46 | case String s: |
4848
| Test.java:1:14:1:17 | Test | 47 | break; |
49-
| Test.java:1:14:1:17 | Test | 48 | case R(S(x), y): |
49+
| Test.java:1:14:1:17 | Test | 48 | case R(S(int x), String y): |
5050
| Test.java:1:14:1:17 | Test | 49 | break; |
5151
| Test.java:1:14:1:17 | Test | 50 | default: |
5252
| Test.java:1:14:1:17 | Test | 51 | break; |
5353
| Test.java:1:14:1:17 | Test | 52 | } |
5454
| Test.java:1:14:1:17 | Test | 53 | switch (o) { |
5555
| Test.java:1:14:1:17 | Test | 54 | case String s -> { |
5656
| Test.java:1:14:1:17 | Test | 55 | } |
57-
| Test.java:1:14:1:17 | Test | 56 | case R(S(x), y) -> { |
57+
| Test.java:1:14:1:17 | Test | 56 | case R(S(int x), String y) -> { |
5858
| Test.java:1:14:1:17 | Test | 57 | } |
5959
| Test.java:1:14:1:17 | Test | 58 | case default -> { |
6060
| Test.java:1:14:1:17 | Test | 59 | } |
6161
| Test.java:1:14:1:17 | Test | 60 | } |
6262
| Test.java:1:14:1:17 | Test | 61 | var a = switch (o) { |
6363
| Test.java:1:14:1:17 | Test | 62 | case String s: |
6464
| Test.java:1:14:1:17 | Test | 63 | yield 1; |
65-
| Test.java:1:14:1:17 | Test | 64 | case R(S(x), y): |
65+
| Test.java:1:14:1:17 | Test | 64 | case R(S(int x), String y): |
6666
| Test.java:1:14:1:17 | Test | 65 | yield x; |
6767
| Test.java:1:14:1:17 | Test | 66 | case default: |
6868
| Test.java:1:14:1:17 | Test | 67 | yield 2; |
6969
| Test.java:1:14:1:17 | Test | 68 | }; |
7070
| Test.java:1:14:1:17 | Test | 69 | var b = switch (o) { |
7171
| Test.java:1:14:1:17 | Test | 70 | case String s -> 1; |
72-
| Test.java:1:14:1:17 | Test | 71 | case R(S(x), y) -> x; |
72+
| Test.java:1:14:1:17 | Test | 71 | case R(S(int x), String y) -> x; |
7373
| Test.java:1:14:1:17 | Test | 72 | default -> 2; |
7474
| Test.java:1:14:1:17 | Test | 73 | }; |
7575
| Test.java:1:14:1:17 | Test | 74 | if (o instanceof String s) { |
7676
| Test.java:1:14:1:17 | Test | 75 | } |
77-
| Test.java:1:14:1:17 | Test | 76 | if (o instanceof R(S(x), y)) { |
77+
| Test.java:1:14:1:17 | Test | 76 | if (o instanceof R(S(int x), String y)) { |
7878
| Test.java:1:14:1:17 | Test | 77 | } |
79-
| Test.java:1:14:1:17 | Test | 78 | } |
80-
| Test.java:1:14:1:17 | Test | 79 | } |
79+
| Test.java:1:14:1:17 | Test | 78 | switch (o) { |
80+
| Test.java:1:14:1:17 | Test | 79 | case R(S(var x), var y) -> { |
81+
| Test.java:1:14:1:17 | Test | 80 | } |
82+
| Test.java:1:14:1:17 | Test | 81 | case default -> { |
83+
| Test.java:1:14:1:17 | Test | 82 | } |
84+
| Test.java:1:14:1:17 | Test | 83 | } |
85+
| Test.java:1:14:1:17 | Test | 84 | if (o instanceof R(S(var x), var y)) { |
86+
| Test.java:1:14:1:17 | Test | 85 | } |
87+
| Test.java:1:14:1:17 | Test | 86 | } |
88+
| Test.java:1:14:1:17 | Test | 87 | } |

0 commit comments

Comments
 (0)