-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Java: Add query to detect non-case labels in switch statements #19998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+85
−0
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## Overview | ||
|
||
Java allows to freely mix `case` labels and ordinary statement labels in the body of | ||
a `switch` statement. However, this is confusing to read and may be the result of a typo. | ||
|
||
## Recommendation | ||
|
||
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. | ||
|
||
## Example | ||
|
||
```java | ||
public class Test { | ||
void test_noncase_label_in_switch(int p) { | ||
switch (p) { | ||
case 1: // Compliant | ||
case2: // Non-compliant, likely a typo | ||
break; | ||
case 3: | ||
notcaselabel: // Non-compliant, confusing to read | ||
for (;;) { | ||
break notcaselabel; | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
In the example, `case2` is most likely a typo and should be fixed. For the intensional `notcaselabel`, placing the labelled code into a function and then calling that function is more readable. | ||
|
||
## References | ||
|
||
Similar to the JS CodeQL query - [js/label-in-switch](https://codeql.github.com/codeql-query-help/javascript/js-label-in-switch/). | ||
tamasvajk marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @id java/label-in-switch | ||
* @name Non-case label in switch statement | ||
* @description A non-case label appearing in a switch statement | ||
* is confusing to read or may even indicate a bug. | ||
* @previous-id java/label-in-case | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity recommendation | ||
* @tags quality | ||
* maintainability | ||
* readability | ||
*/ | ||
|
||
import java | ||
|
||
from LabeledStmt l, SwitchStmt s, string alert | ||
where | ||
l = s.getAStmt+() and | ||
if exists(JumpStmt jump | jump.getTargetLabel() = l) | ||
then alert = "Confusing non-case label in switch statement." | ||
else | ||
alert = | ||
"Possibly erroneous non-case label in switch statement. The case keyword might be missing." | ||
select l, alert |
2 changes: 2 additions & 0 deletions
2
java/ql/test/query-tests/LabelInSwitch/LabelInSwitch.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
query: Language Abuse/LabelInSwitch.ql | ||
postprocess: utils/test/InlineExpectationsTestQuery.ql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.