Skip to content

Commit 5cb5ee0

Browse files
committed
Fix pretty-printing of anonymous vars and multiple patterns; add test
1 parent f317f78 commit 5cb5ee0

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ private class PpInstanceOfExpr extends PpAst, InstanceOfExpr {
400400

401401
private class PpLocalVariableDeclExpr extends PpAst, LocalVariableDeclExpr {
402402
override string getPart(int i) {
403-
i = 0 and result = this.getName()
403+
i = 0 and
404+
(if this.isAnonymous() then result = "_" else result = this.getName())
404405
or
405406
i = 1 and result = " = " and exists(this.getInit())
406407
}
@@ -793,7 +794,11 @@ private class PpPatternCase extends PpAst, PatternCase {
793794
or
794795
i = base + 2 and
795796
this.getPatternAtIndex(n) instanceof LocalVariableDeclExpr and
796-
not this.isAnonymousPattern(n) and
797+
(
798+
exists(this.getPatternAtIndex(n).asBindingOrUnnamedPattern().getTypeAccess())
799+
or
800+
not this.isAnonymousPattern(n)
801+
) and
797802
result = " "
798803
or
799804
i = base + 3 and
@@ -815,9 +820,10 @@ private class PpPatternCase extends PpAst, PatternCase {
815820

816821
override PpAst getChild(int i) {
817822
exists(int n, int base | exists(this.getPatternAtIndex(n)) and base = n * 4 |
818-
i = 1 and result = this.getPatternAtIndex(n).asBindingOrUnnamedPattern().getTypeAccess()
823+
i = base + 1 and
824+
result = this.getPatternAtIndex(n).asBindingOrUnnamedPattern().getTypeAccess()
819825
or
820-
i = 1 and result = this.getPatternAtIndex(n).asRecordPattern()
826+
i = base + 1 and result = this.getPatternAtIndex(n).asRecordPattern()
821827
)
822828
or
823829
exists(int base | base = (max(int n | exists(this.getPatternAtIndex(n))) + 1) * 4 |

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ case R(S(var x), var y) -> { }
4545

4646
if (o instanceof R(S(var x), var y)) { }
4747

48+
switch(o) {
49+
case String _, Integer _:
50+
case R(S(_), _):
51+
default:
52+
break;
53+
}
54+
4855
}
4956

5057
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
//semmle-extractor-options: --javac-args --release 21
1+
//semmle-extractor-options: --javac-args --release 22

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,11 @@
8484
| Test.java:1:14:1:17 | Test | 83 | } |
8585
| Test.java:1:14:1:17 | Test | 84 | if (o instanceof R(S(var x), var y)) { |
8686
| 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 | } |
87+
| Test.java:1:14:1:17 | Test | 86 | switch (o) { |
88+
| Test.java:1:14:1:17 | Test | 87 | case String _, Integer _: |
89+
| Test.java:1:14:1:17 | Test | 88 | case R(S(var _), var _): |
90+
| Test.java:1:14:1:17 | Test | 89 | default: |
91+
| Test.java:1:14:1:17 | Test | 90 | break; |
92+
| Test.java:1:14:1:17 | Test | 91 | } |
93+
| Test.java:1:14:1:17 | Test | 92 | } |
94+
| Test.java:1:14:1:17 | Test | 93 | } |

0 commit comments

Comments
 (0)