2020-06-30 12:48:07 +02:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
|
|
|
|
----
|
|
|
|
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
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
|
|
|
|
----
|
|
|
|
public void Sample(bool b)
|
|
|
|
{
|
|
|
|
bool a = false;
|
|
|
|
if (Foo(a))
|
|
|
|
{
|
|
|
|
DoSomething();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b)
|
|
|
|
{
|
|
|
|
DoSomething();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DoSomethingElse();
|
|
|
|
}
|
|
|
|
|
|
|
|
var d = "xxx";
|
|
|
|
var res = d;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
== Exceptions
|
|
|
|
|
|
|
|
This rule will not raise an issue in either of these cases:
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2021-01-27 13:42:22 +01:00
|
|
|
* When the condition is a single ``++const bool++``
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2020-06-30 12:48:07 +02:00
|
|
|
----
|
|
|
|
const bool debug = false;
|
|
|
|
//...
|
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
// Print something
|
|
|
|
}
|
|
|
|
----
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2021-01-27 13:42:22 +01:00
|
|
|
* When the condition is the literal ``++true++`` or ``++false++``.
|
2020-06-30 12:48:07 +02:00
|
|
|
|
|
|
|
In these cases it is obvious the code is as intended.
|
|
|
|
|
|
|
|
include::../see.adoc[]
|