Bug 176542 - eclipse compiler fails with interesting code case
Summary: eclipse compiler fails with interesting code case
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2.2   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: 3.3 M6   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-03-06 18:54 EST by jon CLA
Modified: 2007-03-07 01:30 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.