GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] AvoidArrayLoops: False negative when break inside switch statement · Issue #5877 · pmd/pmd

[java] AvoidArrayLoops: False negative when break inside switch statement #5877

New issue
Copy link
New issue
Copy link
Closed
#6316
Closed
[java] AvoidArrayLoops: False negative when break inside switch statement#5877
#6316
Copy link
Labels
a:false-negativePMD doesn't flag a problematic piece of code
Milestone
7.20.0

Description

@csrma
csrma
opened on Jul 4, 2025
Issue body actions

Affects PMD Version: 7.15.0

Rule: AvoidArrayLoops

Description:
The AvoidArrayLoops is not triggered when there is a break statement inside a switch-case statement in the loop. In the following code, if we remove/comment out the break statement, the issue is triggered at line 7. I suspect the reason could be that the XPath expression explicitly checks [not(.//BreakStatement)], regardless of whether the break is exiting the loop or the switch statement.

Code Sample demonstrating the issue:

import java.util.Arrays;

class ArrayLoopBugDemo {

    public int[] showBug(int[] source, int condition) {
        int[] destination = new int[source.length];
        for (int i = 0; i < source.length; i++) {    // should raise AvoidArrayLoops here
            destination[i] = source[i];
            switch (condition) {
                case 0:
                    System.out.println("This is an unreachable switch case.");
                    break;
            }
        }
        return destination;
    }

    public static void main(String[] args) {
        ArrayLoopBugDemo demo = new ArrayLoopBugDemo();
        
        int[] dest = demo.showBug(new int[]{1, 2, 3, 4, 5}, 1);
    }
}

Expected outcome:

PMD should report a violation at line 7, but doesn't. This is a false-negative.

Running PMD through: [CLI]

Activity

  1. csrma
    added
    a:false-negativePMD doesn't flag a problematic piece of code
    on Jul 4, 2025
  2. adangel
    changed the title [-][java] AvoidArrayLoops false-negative when break inside switch statement[/-] [+][java] AvoidArrayLoops: False negative when break inside switch statement[/+] on Dec 24, 2025
  3. adangel
    added this to the 7.20.0 milestone on Dec 24, 2025
  4. adangel
    closed this as completedin #6316on Dec 24, 2025
  5. adangel
    added 2 commits that reference this issue on Dec 24, 2025

    [doc] Update release notes (#5877, #6316)

    23dc686

    [java] Fix #5877: AvoidArrayLoops false-negative when break inside sw…

    abc213a
  6. wener-tiobe
    added a commit that references this issue on Feb 10, 2026

    RM-37163: CPD 7.21.0 upgrade (#8)

    6a1959a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.20.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.