Is this a resource leak or a false positive?

Eclipse gives me a warning about declaring "out". Is this false positive?

Random r = new Random(); try(PrintWriter out1 = new PrintWriter("one.txt"); PrintWriter out2 = new PrintWriter("two.txt")) { PrintWriter out = r.nextBoolean()?out1:out2; out.println("x"); } 

PS: Warning: "resource leak:" output "never closes."

+7
source share
2 answers

This is false positive. All instances are properly closed.

I disabled these resource related warnings in Eclipse for a long time. They are really unreliable because there are so many “apparently” correct control flow paths that cannot be identified as “correct” Eclipse without actually executing them ... Any non-trivial code will be doomed to have these false positives.

+5
source

Definitely false positive , out assigned to out1 or out2 , which automatically closes. In addition, an external invisible try block.

+1
source

All Articles