Affects PMD Version:
Rule:
https://pmd.github.io/pmd/pmd_rules_java_bestpractices.html#unusedassignment
Description:
Code Sample demonstrating the issue:
// SocketServer.java
// ...
public ScheduledFuture<IOException> startThenStopAfter(long delay, TimeUnit unit) {
if (stopRequested.get()) {
return null;
}
synchronized (this) {
start();
ScheduledFuture<?> existingFuture = this.timeoutFuture;
if (existingFuture != null) {
existingFuture.cancel(false);
this.timeoutFuture = null; // violation here
}
return (this.timeoutFuture = TIMEOUTS.schedule(new Callable<IOException>() {
@SuppressFBWarnings("THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION")
@Override
public IOException call() throws Exception {
try {
stop();
return null;
} catch (IOException e) {
return e;
}
}
}, delay, unit));
}
}
Expected outcome:
PMD reports a violation, but that's wrong. That's a false positive.
SocketServer.java:446: UnusedAssignment: The value assigned to field 'timeoutFuture' is never used (overwritten on line 449)
"timeoutFuture" is set to null in line 446 because another thread may be accessing it. While there is a potential short time window between line 445 and 446, there can be a significantly longer time interval between calling TIMEOUTS.schedule and assigning its return value to timeoutFuture in line 449).
We can check timeoutFuture from several threads and multiple methods (stop() and startThenStopAfter()), so this shouldn't be an error (and it wasn't reported by 6.55.0).
Running PMD through: CLI
file and ruleset attached
file-and-ruleset2.zip
Affects PMD Version:
Rule:
https://pmd.github.io/pmd/pmd_rules_java_bestpractices.html#unusedassignment
Description:
Code Sample demonstrating the issue:
Expected outcome:
PMD reports a violation, but that's wrong. That's a false positive.
"timeoutFuture" is set to null in line 446 because another thread may be accessing it. While there is a potential short time window between line 445 and 446, there can be a significantly longer time interval between calling TIMEOUTS.schedule and assigning its return value to timeoutFuture in line 449).
We can check timeoutFuture from several threads and multiple methods (
stop()andstartThenStopAfter()), so this shouldn't be an error (and it wasn't reported by 6.55.0).Running PMD through: CLI
file and ruleset attached
file-and-ruleset2.zip