Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 342056

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: CoreAssignee: 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:

Description Markus Keller CLA 2011-04-06 13:36:29 EDT
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();
		}
	}
}
Comment 1 Srikanth Sankaran CLA 2011-04-10 06:39:38 EDT
(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.
Comment 2 Srikanth Sankaran CLA 2011-04-26 06:19:07 EDT
(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 ?
Comment 3 Srikanth Sankaran CLA 2011-04-28 06:08:44 EDT
[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.
Comment 4 Srikanth Sankaran CLA 2011-04-28 06:22:22 EDT
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
Comment 5 Markus Keller CLA 2011-04-28 08:03:46 EDT
(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.
Comment 6 Srikanth Sankaran CLA 2011-06-28 04:17:21 EDT
Verified.