I have the following code:
public String testExitPoints() {
boolean myBoolean = false;
try {
if (getBoolean()) {
return "exit 1";
}
if (getBoolean()) {
throw new RuntimeException();
}
} finally {
myBoolean = true;
}
if (getBoolean()) {
return "exit 2";
}
return "exit 3";
}
public static boolean getBoolean() {
Random rand = new Random();
return rand.nextInt() > 100;
}
Now IntelliJ idea gives me for the second and third invocation of getBoolean()
the following hint:
Condition 'getBoolean()' is always 'false'
Now to my understanding, that is not true, since getBoolean()
can either be true
or false
, depending on the generated random value. Am I missing something here, or is that a bug in IntelliJ Idea?
getBoolean()
than what you posted, or it's a bug in Intellij Idea (in which case you should file a bug report). What do you get when running your code? – LurgetBoolean()
, it's just a test project for this method. Running the code does sometimes returnfalse and sometimes
true`as you would expect. – GlaudiagetBoolean()
wasn't random, IntelliJ would be correct. So I'd guess it is indeed a bug (read as: maybe some kind of incorrect optimization/simplification). – Wingerif(getBoolean())
conditions are never going to be executed. Since you have a return in first condition. What happens if you remove the return statement from first condition? – LorinelorinergetBoolean()
invocation. – Glaudia