Skip to content

Commit

Permalink
Fix FP on S2095 when using @lombok.Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz-tylenda-sonarsource committed Dec 10, 2024
1 parent e98c1cc commit 991f333
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,21 @@ private void closeResource(@Nullable final SymbolicValue target) {
@Override
public void visitIdentifier(IdentifierTree tree) {
// close resource as soon as it is encountered in the resource declaration
if (isWithinTryHeader(tree)) {
// or if it is annotated with @lombok.Cleanup
if (isWithinTryHeader(tree) || isAnnotatedLombokCleanup(tree)) {
Symbol symbol = tree.symbol();
closeResource(programState.getValue(symbol));
}
}

private static boolean isAnnotatedLombokCleanup(IdentifierTree tree) {
return tree
.symbol()
.metadata()
.annotations()
.stream()
.anyMatch(annotation -> "lombok.Cleanup".equals(annotation.symbol().type().fullyQualifiedName()));
}
}

private class PostStatementVisitor extends CheckerTreeNodeVisitor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public void wrongHandling() throws IOException {
}

void withLombokCleanup(String fileName) throws IOException {
// FP
@Cleanup
InputStream in = new FileInputStream(fileName); // Noncompliant
InputStream in = new FileInputStream(fileName);
in.read();
}
}
Expand Down

0 comments on commit 991f333

Please sign in to comment.