Incorrect warning?

I have this code:

boolean isEntityCompletelyValid = true;

boolean isCommonValid = false;

isEntityCompletelyValid = isEntityCompletelyValid && isCommonValid;


IDEA build 859 is showing a "warning" message where it says "Condition isEntityCompletelyValid is always true". This is obviously not the case (note that this is reduced from code that actually calculated something for isCommonValid).

Is this a bug, or am I missing something?

;ted

5 comments
Comment actions Permalink

i have gotten this many times. i usually start walking through the code and trying to find a case where my boolean is not true. Basically I try to come up with a scenerio where idea is wrong. So far i've been wrong EVERY time :(

I would say you should prove that isEntitycomplteyValid can be false.

0
Comment actions Permalink

Ted Young wrote:

I have this code:

boolean isEntityCompletelyValid = true;

boolean isCommonValid = false;

isEntityCompletelyValid = isEntityCompletelyValid && isCommonValid;


IDEA build 859 is showing a "warning" message where it says "Condition
isEntityCompletelyValid is always true". This is obviously not the
case (note that this is reduced from code that actually calculated
something for isCommonValid).


For isCommonValid? Not for isEntityCompletelyValid? In that case, what
you have is approximately the following:

boolean isEntityCompletelyValid = true;
boolean isCommonValid = calculateSomething();
isEntityCompletelyValid = isEntityCompletelyValid && isCommonValid;

If IDEA gives you a warning in the third line, for the second occurrence
of isEntityCompletelyValid (immediately to the right of the assignment
operator), then IDEA is completely correct. The old value is always
true, so what you have is equivalent to the following:

boolean isEntityCompletelyValid = true;
boolean isCommonValid = calculateSomething();
isEntityCompletelyValid = true && isCommonValid;

Or did I misunderstand something in your example? If so, please provide
a more complete example.

0
Comment actions Permalink

IDEA tries to bring good coding style to the masses, eh? ;)
You might want to use isEntityCompletelyValid &= isCommonValid;

Still, I would say that IDEA should not be that strict on style if the same
variable is used as lvalue.
-



"Ted Young" <idea@jitterpig.com> wrote in message
news:bevb81$ahj$1@is.intellij.net...
I have this code:

boolean isEntityCompletelyValid = true;

boolean isCommonValid = false;

isEntityCompletelyValid = isEntityCompletelyValid && isCommonValid;


IDEA build 859 is showing a "warning" message where it says "Condition
isEntityCompletelyValid is always true". This is obviously not the case
(note that this is reduced from code that actually calculated something for
isCommonValid).

Is this a bug, or am I missing something?



0
Comment actions Permalink

Ahh, OK, I see what IDEA is trying to say.

> boolean isEntityCompletelyValid = true;
> boolean isCommonValid = validateCommon( entity );
> isEntityCompletelyValid = isEntityCompletelyValid && isCommonValid;

Could be replaced with:

> boolean isCommonValid = validateCommon( entity );
> boolean isEntityCompletelyValid = true && isCommonValid;

Which reduces to:

> boolean isCommonValid = validateCommon( entity );
> boolean isEntityCompletelyValid = isCommonValid;

So yes, at that point in the code (the first line above),
isEntityCompletelyValid will always have the boolean value of true, since
that's what was assigned previously. I guess there are two problems with this
"warning: It's misleading, because it's saying that on the right side of the
equation, I could replace isEntityCompletelyValid with 'true'. So now what?
It wasn't a very useful warning, because I didn't know what to do to "fix" the
warning. Now I can see that I don't want to replace the assignment in with
(which is silly), but does lead me to , which makes sense. Now, if it
had done a refactoring where it replaced with then I would've
immediately seen what was going on. In fact, the code ends up being reduced
even further to:

boolean isEntityCompletelyValid = validateCommon( entity );

Tossing out the use of isCommonValid! That's where I needed to end up, I just
didn't know it based on the warning.

Thanks for pointing me in the right direction and perhaps this is something
that could be improved.

;ted

"Jonas Kvarnström" <jonkv@ida.liu.se> wrote in message
news:bevc58$bbl$1@is.intellij.net...

Ted Young wrote:

I have this code:

>

boolean isEntityCompletelyValid = true;

>

boolean isCommonValid = false;

>

isEntityCompletelyValid = isEntityCompletelyValid && isCommonValid;

>
>

IDEA build 859 is showing a "warning" message where it says "Condition
isEntityCompletelyValid is always true". This is obviously not the
case (note that this is reduced from code that actually calculated
something for isCommonValid).

>

For isCommonValid? Not for isEntityCompletelyValid? In that case, what
you have is approximately the following:

>

boolean isEntityCompletelyValid = true;
boolean isCommonValid = calculateSomething();
isEntityCompletelyValid = isEntityCompletelyValid && isCommonValid;

>

If IDEA gives you a warning in the third line, for the second occurrence
of isEntityCompletelyValid (immediately to the right of the assignment
operator), then IDEA is completely correct. The old value is always
true, so what you have is equivalent to the following:

>

boolean isEntityCompletelyValid = true;
boolean isCommonValid = calculateSomething();
isEntityCompletelyValid = true && isCommonValid;

>

Or did I misunderstand something in your example? If so, please provide
a more complete example.

>


0
Comment actions Permalink

Ted Young wrote:

> It's misleading, because it's saying that on the right side of the

equation, I could replace isEntityCompletelyValid with 'true'.


But the warning only applies to the highlighted part of the line. Only
the instance of isEntityCompletelyValid on the right side of the
assignment is highlighted as having a constant value.

Although if you turn on additional warnings you might get overlapping
warnings and then it's difficult to see which warning applies where. Is
that what happened?

0

Please sign in to leave a comment.