You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] [7.0-rc1] UnusedAssignment for used field #4435
Description:
A class field is not the same as a method variable as it is in scope for other method calls. This also only restores the value when an unhandled error occurs to rollback the state, so the detection is misunderstanding the behavior.
Not sure if I should open a different issue, but I've noticed a very similar false-positive for UnusedAssigment in cases where it assumes that something won't be null in the control flow that follows (no exception catching involved).
Here is a full example (slightly contrived), using maven-pmd-plugin version 3.20.1-pmd-7-SNAPSHOT configured with PMD 7.0.0-rc2:
This flags the assignment at line 24 (testResult = -1;) as UnusedAssigment. That's of course not necessarily the case if testList is indeed null.
I've also seen this false positive in a piece of code that goes through a conditional while loop in a similar way, along these lines:
// Sample data structurepublicclassNode {
Stringvalue;
NodenextNode;
}
publicclassExample {
privateStringmatchingEntry;
privateStringgetMatchingEntry() { returnmatchingEntry; }
privatevoidfindPosition() {
matchingEntry = null; // This is flagged as UnusedAssigment, even if it doesn't know the result of loadNodeChain()Nodehead = loadNodeChain(); // May or may not be null...while (head != null) {
if (head.getValue() != null && head.getValue().startsWith("a")) {
matchingEntry = head.getValue();
break;
}
head = head.nextNode();
}
}
}
Activity
harbulot commentedon May 10, 2023
Not sure if I should open a different issue, but I've noticed a very similar false-positive for
UnusedAssigmentin cases where it assumes that something won't be null in the control flow that follows (no exception catching involved).Here is a full example (slightly contrived), using
maven-pmd-pluginversion 3.20.1-pmd-7-SNAPSHOT configured with PMD 7.0.0-rc2:This flags the assignment at line 24 (
testResult = -1;) asUnusedAssigment. That's of course not necessarily the case iftestListis indeednull.I've also seen this false positive in a piece of code that goes through a conditional
whileloop in a similar way, along these lines:Merge pull request #4535 from oowekyala:issue4435-unusedAssignment