How Does Resharper Know “Expression is always true”?

前端 未结 4 1400
既然无缘
既然无缘 2020-12-11 15:41

Check out the following code:

private void Foo(object bar)
{
   Type type = bar.GetType();

    if (type != null) // Expression is always true
    {   
    }         


        
4条回答
  •  攒了一身酷
    2020-12-11 16:07

    Yes, it basically has knowledge of some well-known methods. You should find the same for string concatenation too, for example:

    string x = null;
    string y = null;
    string z = x + y;
    
    if (z == null)
    {
        // ReSharper should warn about this never executing
    }
    

    Now the same information is also becoming available via Code Contracts - I don't know whether JetBrains is hooking directly into this information, has its own database, or a mixture of the two.

提交回复
热议问题