eclipse.jdt.ls
eclipse.jdt.ls copied to clipboard
false alarm of Resource leak warning
Given below piece of code, JDT.LS issues a false alarm of "Resource leak".
import java.util.Scanner;
public class Sample {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int i = 0;
for (i = 0; i < 1; i++) {
}
reader.close();
}
}

It's reproduced in both VS Code and Eclipse.
More clues:
- If we define
iinside for-loop asfor (int i =0; i<1; i++)..., the warning is gone. - In IntelliJ IDEA 2020 or some older version Eclipse e.g. 2019-03 (4.11.0), no such warning.
- Same issue discussed in a reddit thread 9 months ago.
Looking through JDT bugzilla, I see quite a few cases over the years of unexpected resource leaks reported. None seem as straightforward as this though. I think our best bet is to file these upstream, much like https://bugs.eclipse.org/bugs/show_bug.cgi?id=561589 (redhat-developer/vscode-java#1349) .