Conditional expressions which are always true or false can lead to dead code. Such code is always buggy and should never
be used in production.
public void Sample(bool b)
{
bool a = false;
if (a) // Noncompliant
{
DoSomething(); // never executed
}
if (!a || b) // Noncompliant; "!a" is always "true", "b" is never evaluated
{
DoSomething();
}
else
{
DoSomethingElse(); // never executed
}
var d = "xxx";
var res = d ?? "value"; // Noncompliant, d is always not null, "value" is never used
}
public void Sample(bool b)
{
bool a = false;
if (Foo(a))
{
DoSomething();
}
if (b)
{
DoSomething();
}
else
{
DoSomethingElse();
}
var d = "xxx";
var res = d;
}
This rule will not raise an issue in either of these cases:
const bool
const bool debug = false;
//...
if (debug)
{
// Print something
}
true or false. In these cases it is obvious the code is as intended.