Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: Finding Bugs: if(false)

I've just found a bug in my program (after some amount of debugging and tearing my hair)

bool first = true;

foreach (RdAbstractNode node in listNodes)
{
    if (!first)
    {
        // do stuff (does not change first)
    }
    // do more stuff (does not change first)
}

As you can see firstis always true - never changed. So if(!first) is basically if(false).
The compiler did not generate a warning although it is set to level 4 (highest level).

How can I find similar if(false) errors?

I'm using VS 2010, .Net 4.0 compiler, project setting .Net 2.0

like image 989
Simon Ottenhaus Avatar asked Apr 20 '11 13:04

Simon Ottenhaus


2 Answers

With Resharper you'll get warnings like this

enter image description here

like image 196
Bala R Avatar answered Sep 27 '22 16:09

Bala R


I don't know of a compiler feature that will help you - you need unit tests.

like image 34
dahlbyk Avatar answered Sep 27 '22 15:09

dahlbyk