Skip to content

Commit

Permalink
[java] Fix pmd#5067: CloseResource: False positive for FileSystems.ge…
Browse files Browse the repository at this point in the history
…tDefault() (pmd#5225)

Merge pull request pmd#5225 from lukasgraef:issue5067
  • Loading branch information
adangel committed Oct 4, 2024
2 parents 07cd250 + d2c42d2 commit 5ce4d29
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
Expand Down Expand Up @@ -204,6 +205,7 @@ private Map<ASTVariableId, TypeNode> getResourceVariables(ASTExecutableDeclarati
.filter(this::isVariableNotSpecifiedInTryWithResource)
.filter(var -> isResourceTypeOrSubtype(var) || isNodeInstanceOfResourceType(getTypeOfVariable(var)))
.filterNot(var -> var.isAnnotationPresent("lombok.Cleanup"))
.filterNot(this::isDefaultFileSystem)
.toList();

for (ASTVariableId var : vars) {
Expand Down Expand Up @@ -499,6 +501,12 @@ private boolean isVariableNotSpecifiedInTryWithResource(ASTVariableId varId) {
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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 5ce4d29

Please sign in to comment.