Skip to content
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] Improve MissingBreakInSwitch #2894

Closed
oowekyala opened this issue Nov 2, 2020 · 1 comment
Closed

[java] Improve MissingBreakInSwitch #2894

oowekyala opened this issue Nov 2, 2020 · 1 comment
Labels
a:false-negative PMD doesn't flag a problematic piece of code an:enhancement An improvement on existing features / rules
Projects
Milestone

Comments

@oowekyala
Copy link
Member

oowekyala commented Nov 2, 2020

Is your feature request related to a problem? Please describe.

Problem 1: false-negatives

MissingBreakInSwitch currently only checks that there are as many breaks as there are cases. This fails on eg

switch (a) {
case 1:
  doSomethingWithoutBreak(a); // falls through
case 3:
  for (...) { if (...) break; } //breaks the loop, but falls through
case 5:
  if (...) break;
  else if (...) break;
  else { /* no break = fallthrough */ }
case 6:
  break;
}

The above produces no violation as it has 4 cases and 4 breaks. However, all of those cases may fall through to next one (except the last obviously). This is because the strategy of the rule is just to count break statements.

Problem 2: naming

I think the rule's purpose is to avoid implicit fall through to the next case, as this may be unexpected. However the rule's name just describes its implementation strategy, and not particularly well, as fallthrough may also be avoided by continue, return and throw statements (cases that the rule tries to handle as well).

--> see #3361

Describe the solution you'd like

I think we should

Describe alternatives you've considered
None

Additional context

This will fix

The UnusedAssignmentRule already implements an exploration pass that follows the control flow of the program. It's very easy to extend to detect whether a switch case has fallen through. In fact, it's more about extending it to give access to the information, as the information is already computed by the rule. I've done this in my branches, see MissingBreakInSwitchRule

@adangel
Copy link
Member

adangel commented Apr 22, 2023

This has been fixed with PMD 7.0.0-rc1.

@adangel adangel closed this as completed Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-negative PMD doesn't flag a problematic piece of code an:enhancement An improvement on existing features / rules
Projects
No open projects
PMD 7
  
Done
Development

No branches or pull requests

2 participants