Skip to content

[java] A false positive negative by AvoidBranchingStatementAsLastInLoop when in a nested switch #3438

@Noah0120

Description

@Noah0120

Affects PMD Version:
3.6.0

Rule:
AvoidBranchingStatementAsLastInLoop
Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_errorprone.html#avoidbranchingstatementaslastinloop

Description:
In the switch statement, continue keywords are detected
even if it is not located last in the loop.

Code Sample demonstrating the issue:

class AvoidBranchingStatement{
        public void test(){
        //this source code is from apache/netbeans/java/.../data/BreakOrContinue.java
        A: while (1 == 1)
              B: for (int c = 0; c < 100; c++)
                  switch (c) {
                      case 0: break;
                      case 1: break B;
                      case 2: break A;
                      case 3: continue;         //detected
                      case 4: continue A;     //detected
                      case 5: continue B;     //detected
                  }
        }
}

Simpler,

class AvoidBranchingStatement{
        public void test(){
              for (int c = 0; c < 100; c++)
                  switch (c) {
                      case 0: break;
                      case 1: break;
                      case 2: break;
                      case 3: continue;     //detected
                      case 4: continue;     //detected
                      case 5: continue;     //detected
                  }
        }
}

Expected outcome:
Only the code line, case 5: continue; might be detected.

Running PMD through: [CLI]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions