GeezerVison is showing github.com — [java] CloseResource: false positive with reassignment detection · Issue #2977 · pmd/pmd
[java] CloseResource: false positive with reassignment detection #2977DescriptionRule: CloseResource
Upgraded to 6.30.0, having previously been using 6.28.0. Now seeing false positives with the CloseResource rule, with this contrived example:
import java.io.RandomAccessFile;
public class FalsePositive {
private boolean isHundredBytes = false;
public void bar() throws Exception {
RandomAccessFile file = null;
try {
file = new RandomAccessFile("name", "r");
this.isHundredBytes = file.length() == 100; /* violation here... */
} finally {
file.close();
}
}
}Activitydjh82 commented on Dec 15, 2020 Assume it was introduced by #2811 dhalperi commented on Jan 7, 2021 oowekyala commented on Jan 16, 2021 djh82 commented on Feb 1, 2021 I'm not sure this has been fixed; still seeing issues with 6.31.0 Kittera commented on Feb 18, 2021
I'm not sure this has been fixed; still seeing issues with 6.31.0
I'm seeing this myself on 6.31.0, just updated in order to test. Still flagging it even if I immediately close the Scanner.

 oowekyala commented on Feb 18, 2021 Thanks for your feedback I'll try and see what went wrong adangel commented on Feb 26, 2021 @djh82 Thanks, I can reproduce it now. It has something to do with declaring the variable and re-assigning it.
One possible workaround could be (depends on your actual code) to use two try-blocks:
public void bar3() throws Exception {
try {
RandomAccessFile file = new RandomAccessFile("name", "r");
try {
boolean isHundredBytes = file.length() == 100;
} catch (IOException e) {
// no-op
} finally {
file.close();
}
} catch (IOException e) {
// ignored
}
}djh82 commented on Feb 26, 2021 @adangel excellent. Yes, while digging out the code and writing that reproducer, I did fix the error by doing exactly that. Though I think the pattern is the usual paradigm when try-wth-resources is not available (usually with a null-guard and try/catch in the finally too). adangel commented on Feb 26, 2021 @djh82 Unfortunately I looked at the wrong file, I still had the ScannerFalsePositive sample in my test project which triggered a violation, but your file runs fine.
So I still can't reproduce the violation. The CloseResource rule behaves correctly. I tested versions 6.29.0, 6.30.0, 6.31.0, with and without type resolution.
I can provoke a violation, if I set the rule property "closeAsDefaultTarget" to false - then "file.close()" is ignored and the rule rightly says, the file is not closed.
I've attached my sample project: issue-2977-sample.zip Could you please make it fail? djh82 commented on Mar 1, 2021 @adangel OK, I've tested with 6.32.0, and I still get the same issue. I think this should fail:
public class FalsePositive {
private boolean isHundredBytes = false;
public void bar() throws Exception {
RandomAccessFile file = null;
try {
file = new RandomAccessFile("name", "r");
this.isHundredBytes = file.length() == 100;
} finally {
file.close();
}
}
}adangel commented on Mar 1, 2021 @djh82 Thanks for the updates sample, now we are getting somewhere: Updated test project:
issue-2977-sample.zip
The reported violation:
[INFO] PMD Failure: AnotherFalsePositive:10 Rule:CloseResource Priority:3 'file' is reassigned, but the original instance is not closed.
It's present since 6.32.0, 6.31.0, 6.30.0, 6.29.0. It's not reproducible with 6.28.0.
The line, where we display the violation, is actually this one:
this.isHundredBytes = file.length() == 100;
which doesn't make much sense - neither file is declared here nor it is assigned here.... changed the title [-][java] 6.30.0 introduces new false positive in CloseResource rule?[/-] [+][java] CloseResource: false positive with reassignment detection[/+] on Mar 1, 2021added a commit that references this issue on Mar 1, 2021This issue will close once commit b942c50 is merged into the 'main' branch.b942c50
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|