Affects PMD Version: 6.16.0
Rule: CloseResource
Description:
If the local variable holding a stream is not directly initialized but the value is assigned later on, this assigned is considered problematic. But it isn't, since it only assigns a value.
It should be no problem, if the stream is then later on used with a try-with-resources, and there are no other statements before the try-with-resources.
Code Sample demonstrating the issue:
import java.io.*; public class CloseResourceFP { public void check(File outputFile) { final OutputStream os; if (outputFile == null) { os = System.out; } else if (outputFile.isAbsolute()) { os = Files.newOutputStream(outputFile.toPath()); } else { os = Files.newOutputStream(new File(getProject().getBaseDir(), outputFile.toString()).toPath()); } try (Writer writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"))) { renderer.render(cpd.getMatches(), writer); } } }
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