ReSharper suggests a wrong "Expression is always true"

698 Views Asked by At

I found this bug in my current project and then I reproduced in a very simple code, which I share here.

I'm using ReSharper 8.0.2 and Visual Studio 2013.

class Program
{
    static void Main(string[] args)
    {
        var orders = new List<Order> { new Order {ClientId = 10}, new Order()};
        var firstOrder = orders.FirstOrDefault();
        if ( firstOrder != null && firstOrder.ClientId.HasValue)
        {
            // In this line resharper suggests that t.ClientId.HasValue is always true. This is wrong.
            var ordersWithClient = orders.Where(t => t.ClientId.HasValue).ToList();   
        }
    }
}

class Order
{
    public int? ClientId { get; set; }
}
1

There are 1 best solutions below

6
Hemi81 On

Its because you already have a conditional statement that checks if the ClientId has value before before assigning it to a variable. Thus the expression is always true