Strange behaviour?

Build 4069.

import java.util.*;

public class A extends ArrayList]]> {
public int getFirstPozitiveNumber(){
for (final int i : this){
//problem is here: continue is marked as "unnecessary as the last statement
in a loop"
//and accepting the "remove unecessary continue" suggestion
//break the code
if (i<0) continue;
else return i;
}
return 0;
}
}

It is a stupid written piece of code ... but the question remains :)


5 comments

I haven't tested, but perhaps it forgets to insert braces or something
else to mark the fact that if i]]> {
public int getFirstPozitiveNumber(){
for (final int i : this){
if (i<0) {}
else return i;
}
return 0;
}
}

0

Dudu <ionut.pascaniuc@viper-systems.com> wrote:

Build 4069.

import java.util.*;

public class A extends ArrayList<Integer> {
public int getFirstPozitiveNumber(){
for (final int i : this){
//problem is here: continue is marked as "unnecessary as the last statement
in a loop"
//and accepting the "remove unecessary continue" suggestion
//break the code
if (i<0) continue;
else return i;
}
return 0;
}
}

It is a stupid written piece of code ... but the question remains :)


What exactly are you trying to do? What's "for (final int i : this)"?

Did you want to do something like (values is where you store your
numbers):

= 0) {
       return i;
     }
  }
  String msg = "hey dude, there's no positive number";
  throw new NoSuchElementException(msg);
}
]]>


As the inspection says -- the continue isn't necessary in your code
because it is the last statement in the loop anyway. Which code is
created by the intention? Looks like a bug if it breaks the code...

Hope that helps,

Dirk Dittert

0

Jonas Kvarnström <jonkv@ida.liu.se> wrote:

if (i<0) {}
else return i;


But it's kind of pointless to do it that way...

Best,

Dirk Dittert

0

Dirk Dittert wrote:

Jonas Kvarnström <jonkv@ida.liu.se> wrote:

>> if (i<0) {}
>> else return i;


But it's kind of pointless to do it that way...


Sure it is, but it isn't the task of the "unnecessary continue
statement" inspection to know how to rewrite the if statement, right?
It should only remove the continue statement; then an "empty if clause"
inspection could mark this result and offer to simplify it further.

0

The quick fix shouldn't be breaking the code. Please submit a bug report: http://www.jetbrains.net/jira/browse/IDEA

Bas

0

Please sign in to leave a comment.