Java: Add query to detect non-case labels in switch statements

This commit is contained in:
Tamas Vajk
2025-07-08 14:53:39 +02:00
parent 52abf3ba02
commit 5f7d746266
7 changed files with 85 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
| Test.java:14:17:14:31 | <Label>: ... | Possibly erroneous non-case label in switch statement. The case keyword might be missing. |
| Test.java:17:17:17:39 | <Label>: ... | Confusing non-case label in switch statement. |

View File

@@ -0,0 +1,2 @@
query: Language Abuse/LabelInSwitch.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql

View File

@@ -0,0 +1,21 @@
public class Test {
void test_case_label_only_in_switch(int p) {
switch (p) {
case 1:
case 2:
break;
}
notcaselabelnotinswitch: for (;;) {}
}
void test_noncase_label_in_switch(int p) {
switch (p) {
case 1:
notcaselabel1:; // $ Alert | Possibly erroneous non-case label in switch statement. The case keyword might be missing.
break;
case 2:
notcaselabel2: for (;;) { break notcaselabel2; } // $ Alert | Confusing non-case label in switch statement.
break;
}
}
}