-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[java] UseTryWithResources false positive when AutoCloseable helper used #2650
Comments
Maybe the problem is, that your helper class The rule searches for any method call with the name |
Actually AutoClosable logic is used for another case. |
I actually disagree - if the thing, that is being closed, is AutoCloseable, then one should use try-with-resources. That's the point. If you use IOUtils.closeQuietly(resource), the rule should still trigger, even though the close method is static. We have two cases:
Currently, the rule always checks both for the type "AutoCloseable" - the thing, on which the close method is called (might be the resource or in static case the helper class) and also (if present) the argument. The rule should probably be changed to check the argument - if present (and assume, it's a static method call) and otherwise check the method call target. The rule is a XPath rule: pmd/pmd-java/src/main/resources/category/java/bestpractices.xml Lines 1839 to 1845 in 9ed79f4
The type check in line 1842 should probably only be done, if there are no arguments. |
This has been fixed with PMD 7.0.0-rc1. |
Affects PMD Version:
maven-pmd-plugin 3.13.0 (pmd 6.21.0)
Rule: UseTryWithResources
Description:
PMD Failure: UseTryWithResources when resource is not AutoCloseable and closed via external class which implement AutoCloseable
Code Sample demonstrating the issue:
Steps to reproduce:
Running PMD through: Maven
The text was updated successfully, but these errors were encountered: