Repository navigation
Closed
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
Description
The Maven PMD plugin reports UnusedAssignment false positives about the ++ unary operator.
Affects PMD Version:
7.18.0 and 7.17.0.
Rule:
Description:
Run:
git clone https://github.com/apache/commons-jexl.git
cd commons-jexl
git checkout 3e8bf7b293dc9877de00df1e0d58a0a2ad488fe9
mvn pmd:check -Dcommons.pmd-impl.version=7.18.0
Reports:
<violation beginline="881" endline="881" begincolumn="70" endcolumn="74" rule="UnusedAssignment" ruleset="Best Practices" package="org.apache.commons.jexl3.internal" class="Debugger" method="visit" variable="vars" externalInfoUrl="https://docs.pmd-code.org/pmd-doc-7.18.0/pmd_rules_java_bestpractices.html#unusedassignment" priority="3">
The updated value of variable 'nc' is never used
</violation>
<violation beginline="882" endline="882" begincolumn="75" endcolumn="79" rule="UnusedAssignment" ruleset="Best Practices" package="org.apache.commons.jexl3.internal" class="Debugger" method="visit" variable="predicate" externalInfoUrl="https://docs.pmd-code.org/pmd-doc-7.18.0/pmd_rules_java_bestpractices.html#unusedassignment" priority="3">
The updated value of variable 'nc' is never used
</violation>
<violation beginline="884" endline="884" begincolumn="70" endcolumn="74" rule="UnusedAssignment" ruleset="Best Practices" package="org.apache.commons.jexl3.internal" class="Debugger" method="visit" variable="step" externalInfoUrl="https://docs.pmd-code.org/pmd-doc-7.18.0/pmd_rules_java_bestpractices.html#unusedassignment" priority="3">
The updated value of variable 'nc' is never used
</violation>Code Sample demonstrating the issue:
@Override
protected Object visit(final ASTForeachStatement node, final Object data) {
final int form = node.getLoopForm();
builder.append("for (");
final JexlNode body;
if (form == 0) {
// for( .. : ...)
accept(node.jjtGetChild(0), data);
builder.append(" : ");
accept(node.jjtGetChild(1), data);
builder.append(") ");
body = node.jjtGetNumChildren() > 2? node.jjtGetChild(2) : null;
} else {
// for( .. ; ... ; ..)
int nc = 0;
// first child is var declaration(s)
final JexlNode vars = (form & 1) != 0 ? node.jjtGetChild(nc++) : null;
final JexlNode predicate = (form & 2) != 0 ? node.jjtGetChild(nc++) : null;
// the loop step
final JexlNode step = (form & 4) != 0 ? node.jjtGetChild(nc++) : null;
// last child is body
body = (form & 8) != 0 ? node.jjtGetChild(nc) : null;
if (vars != null) {
accept(vars, data);
}
builder.append("; ");
if (predicate != null) {
accept(predicate, data);
}
builder.append("; ");
if (step != null) {
accept(step, data);
}
builder.append(") ");
}
// the body
if (body != null) {
accept(body, data);
} else {
builder.append(';');
}
return data;
}Expected outcome:
PMD reports a violation at lines 881, 882, and 884, but that's wrong. That's a false positive.
Running PMD through: Maven
openjdk version "21.0.9" 2025-10-21
OpenJDK Runtime Environment Homebrew (build 21.0.9)
OpenJDK 64-Bit Server VM Homebrew (build 21.0.9, mixed mode, sharing)
Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)
Maven home: /opt/homebrew/Cellar/maven/3.9.11/libexec
Java version: 21.0.9, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk@21/21.0.9/libexec/openjdk.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "15.7.1", arch: "aarch64", family: "mac"
****.local 24.6.0 Darwin Kernel Version 24.6.0: Mon Aug 11 21:15:09 PDT 2025; root:xnu-11417.140.69.701.11~1/RELEASE_ARM64_T6041 arm64
Docker version 28.4.0, build d8eb465
Metadata
Metadata
Assignees
Labels
a:false-positivePMD flags a piece of code that is not problematic
Activity
[-]False positive `UnusedAssignment` about the ++ unary operator[/-][+][java] False positive `UnusedAssignment` about the ++ unary operator[/+][-][java] False positive `UnusedAssignment` about the ++ unary operator[/-][+][java] UnusedAssignment: False positive about the ++ unary operator[/+]UncleOwen commentedon Jun 24, 2026
Smaller reproducer:
xis unused,iis used - but the message still mentionsi.