Skip to content

Commit 1351f57

Browse files
authored
Merge pull request #19998 from tamasvajk/quality/label-in-switch
Java: Add query to detect non-case labels in switch statements
2 parents 054bbc2 + 5edb60e commit 1351f57

File tree

7 files changed

+85
-0
lines changed

7 files changed

+85
-0
lines changed

java/ql/integration-tests/java/query-suite/java-code-quality-extended.qls.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ql/java/ql/src/Compatibility/JDK9/UnderscoreIdentifier.ql
66
ql/java/ql/src/DeadCode/UselessParameter.ql
77
ql/java/ql/src/Language Abuse/EmptyMethod.ql
88
ql/java/ql/src/Language Abuse/IterableIterator.ql
9+
ql/java/ql/src/Language Abuse/LabelInSwitch.ql
910
ql/java/ql/src/Language Abuse/TypeVariableHidesType.ql
1011
ql/java/ql/src/Language Abuse/UselessNullCheck.ql
1112
ql/java/ql/src/Language Abuse/UselessTypeTest.ql

java/ql/integration-tests/java/query-suite/java-code-quality.qls.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ql/java/ql/src/Compatibility/JDK9/JdkInternalAccess.ql
55
ql/java/ql/src/Compatibility/JDK9/UnderscoreIdentifier.ql
66
ql/java/ql/src/DeadCode/UselessParameter.ql
77
ql/java/ql/src/Language Abuse/IterableIterator.ql
8+
ql/java/ql/src/Language Abuse/LabelInSwitch.ql
89
ql/java/ql/src/Language Abuse/UselessNullCheck.ql
910
ql/java/ql/src/Language Abuse/UselessTypeTest.ql
1011
ql/java/ql/src/Language Abuse/WrappedIterator.ql
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Overview
2+
3+
Java allows to freely mix `case` labels and ordinary statement labels in the body of
4+
a `switch` statement. However, this is confusing to read and may be the result of a typo.
5+
6+
## Recommendation
7+
8+
Examine the non-`case` labels to see whether they were meant to be `case` labels. If not, consider placing the non-`case` label headed code into a function, and use a function call inline in the `switch` body instead.
9+
10+
## Example
11+
12+
```java
13+
public class Test {
14+
void test_noncase_label_in_switch(int p) {
15+
switch (p) {
16+
case 1: // Compliant
17+
case2: // Non-compliant, likely a typo
18+
break;
19+
case 3:
20+
notcaselabel: // Non-compliant, confusing to read
21+
for (;;) {
22+
break notcaselabel;
23+
}
24+
}
25+
}
26+
}
27+
```
28+
29+
In the example, `case2` is most likely a typo and should be fixed. For the intentional `notcaselabel`, placing the labelled code into a function and then calling that function is more readable.
30+
31+
## References
32+
33+
CodeQL query help for JavaScript and TypeScript - [Non-case label in switch statement](https://codeql.github.com/codeql-query-help/javascript/js-label-in-switch/).
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @id java/label-in-switch
3+
* @name Non-case label in switch statement
4+
* @description A non-case label appearing in a switch statement
5+
* is confusing to read or may even indicate a bug.
6+
* @previous-id java/label-in-case
7+
* @kind problem
8+
* @precision very-high
9+
* @problem.severity recommendation
10+
* @tags quality
11+
* maintainability
12+
* readability
13+
*/
14+
15+
import java
16+
17+
from LabeledStmt l, SwitchStmt s, string alert
18+
where
19+
l = s.getAStmt+() and
20+
if exists(JumpStmt jump | jump.getTargetLabel() = l)
21+
then alert = "Confusing non-case label in switch statement."
22+
else
23+
alert =
24+
"Possibly erroneous non-case label in switch statement. The case keyword might be missing."
25+
select l, alert
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| Test.java:14:17:14:31 | <Label>: ... | Possibly erroneous non-case label in switch statement. The case keyword might be missing. |
2+
| Test.java:17:17:17:39 | <Label>: ... | Confusing non-case label in switch statement. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Language Abuse/LabelInSwitch.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Test {
2+
void test_case_label_only_in_switch(int p) {
3+
switch (p) {
4+
case 1:
5+
case 2:
6+
break;
7+
}
8+
notcaselabelnotinswitch: for (;;) {}
9+
}
10+
11+
void test_noncase_label_in_switch(int p) {
12+
switch (p) {
13+
case 1:
14+
notcaselabel1:; // $ Alert | Possibly erroneous non-case label in switch statement. The case keyword might be missing.
15+
break;
16+
case 2:
17+
notcaselabel2: for (;;) { break notcaselabel2; } // $ Alert | Confusing non-case label in switch statement.
18+
break;
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)