Affects PMD Version:
6.34 (did not happen in 6.29)
Rule: UseTryWithResources
Description:
UseTryWithResources
flags consumer methods getting a Closeable
by argument, with a contract to consume it and close it. However, the Closeable
cannot be put in a try-with-resources, as it's not instatiated in the method itself.
We could debate whether the caller should be entrusted to perform the closing, I'd agree it would be better... but the check should still not report this, a separate check should be developed to enforce this kind of symmetry.
Code Sample demonstrating the issue:
Sample code from GeoTools (FeatureIterator
is a Closeable
):
@SuppressWarnings("PMD.UseTryWithResources") public static int count(FeatureIterator<?> iterator) { int count = 0; if (iterator != null) { try { while (iterator.hasNext()) { @SuppressWarnings("unused") Feature feature = iterator.next(); count++; } return count; } finally { iterator.close(); } } return count; }
Steps to reproduce:
Any method of running PMD should produce the issue. In our case, it's run via the maven plugin.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4