New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[java] [7.0-rc1] UnusedAssignment for used field #4435
Comments
Not sure if I should open a different issue, but I've noticed a very similar false-positive for Here is a full example (slightly contrived), using package com.example;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;
public class Example {
private int testResult;
private List<Integer> testList;
public int getTestResult() {
return testResult;
}
public List<Integer> getTestList() {
return testList;
}
public void setTestList(List<Integer> testList) {
this.testList = testList;
}
public void run() {
testResult = -1; // This is flagged as UnusedAssigment
if (testList == null) {
return;
}
int i = 0;
Iterator<Integer> it = testList.iterator();
while(it.hasNext()) {
Integer value = it.next();
if (value != null && value > 10) {
testResult = i;
break;
}
i++;
}
}
} This flags the assignment at line 24 ( I've also seen this false positive in a piece of code that goes through a conditional // Sample data structure
public class Node {
String value;
Node nextNode;
}
public class Example {
private String matchingEntry;
private String getMatchingEntry() { return matchingEntry; }
private void findPosition() {
matchingEntry = null; // This is flagged as UnusedAssigment, even if it doesn't know the result of loadNodeChain()
Node head = 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();
}
}
} |
Affects PMD Version:
7.0-rc1 when upgrading from 6.55
Rule:
UnusedAssignment
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.
Code Sample demonstrating the issue:
Expected outcome:
PMD reports a violation but that should not apply to a class field.
Running PMD through: Gradle
This requires workaround in gradle/gradle#24502
The text was updated successfully, but these errors were encountered: