Check for null parameter in IComparable/CompareTo shows warning.

For the code below, RS shows warning "Expression is always false"/"Code is heuristically unreachable" for the null-check and return expression in CompareTo.
Is this warning correct? Obviously it's possible to send in a null param to the CompareTo method? :/
public class Foo : IComparable<Foo>
{
public int Bar;
public int CompareTo(Foo other)
{
if (other==null)
return 1;
return Bar.CompareTo(other.Bar);
}

public static void Sort()
{
var items = new[]
{
new Foo {Bar = 1},
new Foo {Bar = 2},
null
};
//Safe even with no null-check
items.OrderByDescending(item => item).ToList();
var newObj = new Foo();
var newObj2 = (Foo)null;
//Unsafe if null-check doesn't exist.
newObj.CompareTo(newObj2);
}
}

1 comment
Comment actions Permalink

Hello,

I've raised an issue in our bug-tracker: http://youtrack.jetbrains.net/issue/RSRP-181511
and you're welcome to monitor its status. Thank you!

Andrey Serebryansky
Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

For the code below, RS shows warning "Code is heuristically
unreachable" in the null-check in CompareTo.

Is this warning correct? Obviously it's possible to send in a null
param to the CompareTo method? :/

public
class Foo : IComparable<Foo>
{
public int Bar;
public int CompareTo(Foo other)
{
if (other==null)
return 1;
return Bar.CompareTo(other.Bar);
}
public static void Sort()
{
var items = new[]
{
new Foo {Bar = 1},
new Foo {Bar = 2},
null
};
//Safe even with no null-check
items.OrderByDescending(item => item).ToList();
var newObj = new Foo();
var newObj2 = (Foo)null;
//Unsafe if null-check doesn't exist.
newObj.CompareTo(newObj2);
}
}

 ]]>


---
Original message URL:
http://www.jetbrains.net/devnet/message/5264959#5264959



0

Please sign in to leave a comment.