False positive [Expression is always false] on [x == null] in CompareTo(x, y)

I have seen in the forum several cases where people get a false positive hint or warning that 'Expression is always false/true'. Well, I found another one. I have a custom Comparer-class that implements the IComparer<T>-interface, which has only a single method to implement: CompareTo(T x, T y) : int. In my case, T is a reference type, and following .NET's documentation, I first check the cases where either x or y are null. However, ReSharper keeps telling me that neither x nor y could ever be null:

public int Compare(Shift x, Shift y)
    if (x == y) return 0;
    if (x == null) return -1;
    if (y == null) return 1;

    int startTimeComparison = x.StartTime.CompareTo(y.StartTime);

    return startTimeComparison == 0
                           ? x.EndTime.CompareTo(y.EndTime)
                           : startTimeComparison;

If I omit the checks, FxCop tells me to 'validate my public method's arguments', which I feel is a valid point in this case (not in the least because null-values are valid values when it comes to sorting). So why is ReSharper giving me this message?

I'm using running VS2010 with R# installed.
Comment actions Permalink

Hello Wim
     I'm afraid I wasn't able to reproduce this behavior with the latest ReSharper 6.1.1. Could you please install that version from our web site and check if this still happens for you? Thank you!

Andrey Serebryansky

Senior Support Engineer

JetBrains, Inc


"Develop with pleasure!"

Comment actions Permalink

Hi Andrey,

Thanks for checking. I checked it myself again using the latest version, and the false positive seems to occur only in Silverlight-projects. Can you reproduce it there? I have attached a screenshot so you can see what's actually happening.



Please sign in to leave a comment.