Skip to content

[java] UnusedPrivateMethod false-positive when a private method defined in an inner private class is called. #4016

@garydgregory

Description

@garydgregory

Affects PMD Version: 6.46.0

Rule: UnusedPrivateMethod

Please provide the rule name and a link to the rule documentation:
UnusedPrivateMethod:
https://pmd.github.io/latest/pmd_rules_java_bestpractices.html#unusedprivatemethod

Description:
False-positive when a class calls a private method defined in a private inner class.

Code Sample demonstrating the issue:
You will need Apache Maven installed to build Apache Commons IO: https://maven.apache.org/users/index.html

git clone https://gitbox.apache.org/repos/asf/commons-io.git
cd commons-io
git reset --hard 1bd3a1f35ea95d4847aef44fac5cdb60f366c328
mvn pmd:check

Console output:

[INFO] --- maven-pmd-plugin:3.17.0:check (default-cli) @ commons-io ---
[INFO] PMD version: 6.46.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.608 s
[INFO] Finished at: 2022-06-14T19:31:18-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.17.0:check (default-cli) on project commons-io: You have 1 PMD violation. For more details see: C:\temp\commons-io\target\pmd.xml -> [Help 1]

The unexpected outcome is described in pmd.xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<pmd xmlns="http://pmd.sourceforge.net/report/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd" version="6.46.0" timestamp="2022-06-14T19:14:49.070">
<file name="C:\d3vsrc\git\apache\commons-io\src\main\java\org\apache\commons\io\input\ReversedLinesFileReader.java">
<violation beginline="127" endline="127" begincolumn="24" endcolumn="33" rule="UnusedPrivateMethod" ruleset="Best Practices" package="org.apache.commons.io.input" class="ReversedLinesFileReader$FilePart" method="readLine" externalInfoUrl="https://pmd.github.io/pmd-6.46.0/pmd_rules_java_bestpractices.html#unusedprivatemethod" priority="3">
Avoid unused private methods such as 'readLine()'.
</violation>
</file>
</pmd>

Expected outcome:

PMD reports a violation in ReversedLinesFileReader at line 127, but that's wrong. That's a false positive.

Running PMD through: Maven

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