Skip to content

Commit d0b5b99

Browse files
authored
Merge pull request github#8611 from github/smowton/doc/switch-expr-accessors
Java: make SwitchCase.getRuleExpression/Statement more consistent
2 parents 31ec298 + 81e60eb commit d0b5b99

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,27 @@ class SwitchCase extends Stmt, @case {
438438

439439
/**
440440
* Gets the expression on the right-hand side of the arrow, if any.
441+
*
442+
* Note, this predicate gets a value when this switch case is of the form
443+
* `case e1 -> e2`, where `e2` is neither a block nor a throw statement.
444+
* This predicate is mutually exclusive with `getRuleStatement`.
441445
*/
442-
Expr getRuleExpression() { result.getParent() = this and result.getIndex() = -1 }
446+
Expr getRuleExpression() {
447+
result.getParent() = this and result.getIndex() = -1
448+
or
449+
exists(ExprStmt es | es.getParent() = this and es.getIndex() = -1 | result = es.getExpr())
450+
}
443451

444452
/**
445453
* Gets the statement on the right-hand side of the arrow, if any.
454+
*
455+
* Note, this predicate gets a value when this switch case is of the form
456+
* `case e1 -> { s1; s2; ... }` or `case e1 -> throw ...`.
457+
* This predicate is mutually exclusive with `getRuleExpression`.
446458
*/
447-
Stmt getRuleStatement() { result.getParent() = this and result.getIndex() = -1 }
459+
Stmt getRuleStatement() {
460+
result.getParent() = this and result.getIndex() = -1 and not result instanceof ExprStmt
461+
}
448462
}
449463

450464
/** A constant `case` of a switch statement. */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `SwitchCase.getRuleExpression()` predicate now gets expressions for case rules with an expression on the right-hand side of the arrow belonging to both `SwitchStmt` and `SwitchExpr`, and the corresponding `getRuleStatement()` no longer returns an `ExprStmt` in either case. Previously `SwitchStmt` and `SwitchExpr` behaved differently in
5+
this respect.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class TestSwitchExprStmtConsistency {
2+
3+
static int f() { return 0; }
4+
5+
public static void test(int x) {
6+
7+
// Test that getRuleExpression() and getRuleStatement() behave alike for switch expressions and statements using arrow rules.
8+
9+
switch(x) {
10+
case 1 -> f();
11+
case 2 -> f();
12+
default -> f();
13+
}
14+
15+
int result = switch(x) {
16+
case 1 -> f();
17+
case 2 -> f();
18+
default -> f();
19+
};
20+
21+
}
22+
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
exprs
2+
| TestSwitchExpr.java:9:13:9:41 | case ... | TestSwitchExpr.java:9:43:9:46 | null |
3+
| TestSwitchExpr.java:10:13:10:22 | default | TestSwitchExpr.java:10:24:10:25 | x1 |
4+
| TestSwitchExprStmtConsistency.java:10:7:10:15 | case ... | TestSwitchExprStmtConsistency.java:10:17:10:19 | f(...) |
5+
| TestSwitchExprStmtConsistency.java:11:7:11:15 | case ... | TestSwitchExprStmtConsistency.java:11:17:11:19 | f(...) |
6+
| TestSwitchExprStmtConsistency.java:12:7:12:16 | default | TestSwitchExprStmtConsistency.java:12:18:12:20 | f(...) |
7+
| TestSwitchExprStmtConsistency.java:16:7:16:15 | case ... | TestSwitchExprStmtConsistency.java:16:17:16:19 | f(...) |
8+
| TestSwitchExprStmtConsistency.java:17:7:17:15 | case ... | TestSwitchExprStmtConsistency.java:17:17:17:19 | f(...) |
9+
| TestSwitchExprStmtConsistency.java:18:7:18:16 | default | TestSwitchExprStmtConsistency.java:18:18:18:20 | f(...) |
10+
stmts
11+
| TestSwitchExpr.java:13:13:13:28 | case ... | TestSwitchExpr.java:13:30:13:42 | { ... } |
12+
| TestSwitchExpr.java:14:13:14:22 | default | TestSwitchExpr.java:14:24:14:52 | throw ... |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java
2+
3+
query predicate exprs(SwitchCase sc, Expr e) { e = sc.getRuleExpression() }
4+
5+
query predicate stmts(SwitchCase sc, Stmt s) { s = sc.getRuleStatement() }

0 commit comments

Comments
 (0)