-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[java] Fix #2928: New Rules UnusedLabel and LabeledStatement #6042
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
[java] Fix #2928: New Rules UnusedLabel and LabeledStatement #6042
Conversation
|
Compared to main: (comment created at 2025-10-29 13:42:00+00:00 for 9ca1336) |
|
Thanks for the PR! I've read through the (unfortunately very, very messy) comments in #2928. And it actually suggests two or three rules:
For the rule names, maybe we can find something that does "name the underlying problem" (https://docs.pmd-code.org/latest/pmd_devdocs_major_rule_guidelines.html). For the first rule (this PR), maybe instead of "AvoidLoopLabels" simply just "LabelInLoops". In the rule description, we can then say "Using labels in loops should be avoided as it makes the control flow difficult to understand...". I know, we have a couple of rules that start with an order ("Avoid...", "Use...", "OverrideBoth..."). The second rule could be named "UnusedLabel". An XPath expression, that detect such unused labels, looks like this: The third rule could be named "LabeledStatement" - like the AST node. The XPath is simply The second and third rule could be combined with a property (unusedOnly=true/false): However, I like the simple name "UnusedLabel". What do you (and others) think about the names? Btw. I'm going to rebase your PR/branch... |
1bcca4f to
b569d65
Compare
|
Slight twist on your "LabelInLoops" suggestion: "LabeledLoops". And maybe that is, what the rule should be looking for? |
|
I think there is some overlap between the three rules you propose.
Also I think labels on loops are actually the only legit use case for labels, even though they can be applied to any statement. I think it would be better to have a rule Your XPath expression for detecting unused labels could be simplified, because usages of a label must be descendants of that labeled statement: //LabeledStatement[let $label := @Label return
not( (.//BreakStatement | .//ContinueStatement)[@Label = $label] )
] |
b569d65 to
0f0ee3a
Compare
0f0ee3a to
27a7f2c
Compare
|
@UncleOwen @oowekyala I've updated the description in #2928 and updated this PR. I think, this one is ready to be merge, wdyt? |
pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedLabelTest.java
Outdated
Show resolved
Hide resolved
8271138 to
9ca1336
Compare
Describe the PR
Two new rules as discussed below and summarized on #2928.
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)