[java] Fix #5067: CloseResource: False positive for FileSystems.getDefault() (#5225)

Merge pull request #5225 from lukasgraef:issue5067
This commit is contained in:
Andreas Dangel 2024-10-04 10:06:18 +02:00
commit 5ce4d292fa
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
3 changed files with 25 additions and 0 deletions

View File

@ -32,11 +32,14 @@ The old rule names still work but are deprecated.
### 🐛 Fixed Issues
* java
* [#4532](https://github.com/pmd/pmd/issues/4532): \[java] Rule misnomer for JUnit* rules
* java-errorprone
* [#5067](https://github.com/pmd/pmd/issues/5067): \[java] CloseResource: False positive for FileSystems.getDefault()
### 🚨 API Changes
### ✨ Merged pull requests
* [#4965](https://github.com/pmd/pmd/pull/4965): \[java] Rename JUnit rules with overly restrictive names - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod)
* [#5225](https://github.com/pmd/pmd/pull/5225): \[java] Fix #5067: CloseResource: False positive for FileSystems.getDefault() - [Lukas Gräf](https://github.com/lukasgraef) (@lukasgraef)
* [#5241](https://github.com/pmd/pmd/pull/5241): Ignore javacc code in coverage report - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod)
{% endtocmaker %}

View File

@ -108,6 +108,7 @@ public class CloseResourceRule extends AbstractJavaRule {
.desc("Detect if 'close' (or other closeTargets) is called outside of a finally-block").defaultValue(false).build();
private static final InvocationMatcher OBJECTS_NON_NULL = InvocationMatcher.parse("java.util.Objects#nonNull(_)");
private static final InvocationMatcher FILESYSTEMS_GET_DEFAULT = InvocationMatcher.parse("java.nio.file.FileSystems#getDefault()");
private final Set<String> types = new HashSet<>();
private final Set<String> simpleTypes = new HashSet<>();
@ -204,6 +205,7 @@ public class CloseResourceRule extends AbstractJavaRule {
.filter(this::isVariableNotSpecifiedInTryWithResource)
.filter(var -> isResourceTypeOrSubtype(var) || isNodeInstanceOfResourceType(getTypeOfVariable(var)))
.filterNot(var -> var.isAnnotationPresent("lombok.Cleanup"))
.filterNot(this::isDefaultFileSystem)
.toList();
for (ASTVariableId var : vars) {
@ -499,6 +501,12 @@ public class CloseResourceRule extends AbstractJavaRule {
return tryStatement == null || !isVariableSpecifiedInTryWithResource(varId, tryStatement);
}
private boolean isDefaultFileSystem(ASTVariableId varId) {
@Nullable
ASTExpression initializer = varId.getInitializer();
return FILESYSTEMS_GET_DEFAULT.matchesCall(initializer);
}
private boolean isVariableSpecifiedInTryWithResource(ASTVariableId varId, ASTTryStatement tryWithResource) {
// skip own resources - these are definitively closed
if (tryWithResource.getResources().descendants(ASTVariableId.class).toList().contains(varId)) {

View File

@ -2044,6 +2044,20 @@ public class CastedParameter {
// doesn't need to be closed, it's still a paramter…
}
}
}
]]></code>
</test-code>
<test-code>
<description>#5067: FileSystems.getDefault() can't be closed </description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
public class DefaultFileSystemUsage {
public void useDefaultFS() {
FileSystem defaultFS = FileSystems.getDefault();
}
}
]]></code>
</test-code>