Bug 176542

Summary: eclipse compiler fails with interesting code case
Product: [Eclipse Project] JDT Reporter: jon <whichever>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED INVALID QA Contact:
Severity: normal    
Priority: P3    
Version: 3.2.2   
Target Milestone: 3.3 M6   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description jon CLA 2007-03-06 18:54:08 EST
If I write the following code...

int len = 0;
byte[] buf = new byte[BUFFER];
while ((len = is.read(buf, 0, BUFFER)) != -1)
{
}

The first reference of len is underlined in yellow and the message is: "The local variable len is never read"

If I try to write the code above like this:

byte[] buf = new byte[BUFFER];
while ((int len = is.read(buf, 0, BUFFER)) != -1)
{
}

Then I get a compile error:

Multiple markers at this line:
len cannot be resolved
syntax error on token int, delete this token

Ideally, I'd love to be able to write the code above in such a way as to produce no warnings or errors. =)

Apologies if I have filed this under the wrong product/component... it's difficult to find the correct values without really understanding how this organization is defined.

thanks,

jon
Comment 1 Olivier Thomann CLA 2007-03-06 20:16:11 EST
In this code:
int len = 0;
byte[] buf = new byte[BUFFER];
while ((len = is.read(buf, 0, BUFFER)) != -1)
{
}
You actually don't read the variable len. So if you want to remove the warning, why don't you simply write:
byte[] buf = new byte[BUFFER];
while (is.read(buf, 0, BUFFER) != -1)
{
}

but I would say the right answer is probably to use the value of len in the while block. Otherwise you are simply reading from an input stream without ever doing anything with the data.
Ok to close?
Comment 2 jon CLA 2007-03-06 21:11:19 EST
Good point. closing! =) thanks for the quick response.