7.0.2 inspection problem or am I blind?

I have a problem with inspection saying that the condition is always true. I do not get it... could ypu please confirm that it is an Idea bug, or I should get another coffee?
See attachment

public static int compare(Comparable c1, Comparable c2) {
if (c1==null && c2!=null) return -1;
if (c1!=null && c2==null) return +1;
if (c1==null && c2==null) return 0;
return c1.compareTo(c2);
}



Attachment(s):
inspection.gif
0
3 comments

Hello adam,

Please get another coffee. :) After the check you've done at the first line,
c2 is guaranteed to be null in the third line if c1 is null.

I have a problem with inspection saying that the condition is always
true. I do not get it... could ypu please confirm that it is an Idea
bug, or I should get another coffee?

See attachment

public static int compare(Comparable c1, Comparable c2) {
if (c1==null && c2!=null) return -1;
if (c1!=null && c2==null) return +1;
if (c1==null && c2==null) return 0;
return c1.compareTo(c2);
}

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Our pendant looks a little bit different:

if (c1 == c2) return 0;
if (c1 == null) return -1;
if (c2 == null) return +1;
return c1.compareTo(c2);

0

Hi Dmitry,
Now I see it, isn't it obvious? Coffee to you :)

Tom, your version looks indeed simpler.

Thanks!
Adam

0

Please sign in to leave a comment.