| Summary: | [1.7] Unused variable not detected in resource list of try-with-resources | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Markus Keller <markus.kell.r> |
| Component: | Core | Assignee: | Ayushman Jain <amj87.iitr> |
| Status: | VERIFIED INVALID | QA Contact: | |
| Severity: | normal | ||
| Priority: | P3 | CC: | jarthana, Olivier_Thomann, srikanth_sankaran |
| Version: | 3.7 | ||
| Target Milestone: | 3.7.1 | ||
| Hardware: | PC | ||
| OS: | Windows 7 | ||
| Whiteboard: | |||
(In reply to comment #0) > BETA_JAVA7 > > Unused variable not detected in resource list of try-with-resources: fisA is > flagged as unused but fis is not. Javac flags fis (but not fisA). This is intentional since there is an implicit use with the close call. If as the litmus test for the correctness/usefulness of the warning, we use the criteria that a programmer should be able to go ahead and get rid of this variable (without the meaning of the program being altered), then the current scenario would fail that test. In the current test case I agree that one would expect to see an additional explicit reference. But there could be cases where the try block need not reference the resource handle at all, though it us unclear to me how common those use cases would be. (In reply to comment #1) > In the current test case I agree that one would expect to see an > additional explicit reference. But there could be cases where > the try block need not reference the resource handle at all, I am thinking of acquisition of some kind of lock in the initialization code, executing the try block which makes no reference to the lock and the lock being released in the close call. >though > it us unclear to me how common those use cases would be. Markus, do you disagree with a WONTFIX resolution ? [The code in comment# 0 does not compile anymore with javac as of 7b138, as anticipated by bug 340486] Even if we were to produce a warning, the variable cannot be removed retaining side effects, as the syntax of the resource section calls for a local variable declaration and moreover as there are more side effects than what meets the eye. Note also that we never report an unused warning against a catch parameter. This case is somewhat similar since a declaration inside a catch is a syntactic requirement (In reply to comment #2) > Markus, do you disagree with a WONTFIX resolution ? I wouldn't deny that I don't disagree ;-) The translation of the try-with-resources statement to the pre-Java-7 form indeed makes clear that the value of the the variable is always used. Closing. Verified. |
BETA_JAVA7 Unused variable not detected in resource list of try-with-resources: fisA is flagged as unused but fis is not. Javac flags fis (but not fisA). package p; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class ResourcesTest { public static void main() { try { FileInputStream fisA= new FileInputStream(""); try (FileInputStream fis= new FileInputStream("")) { // fis.available(); } } catch (FileNotFoundException | IOException e) { e.printStackTrace(); } } }