2023-05-03 11:06:20 +02:00
== Why is this an issue?
2020-06-30 12:48:07 +02:00
include::../description.adoc[]
2023-06-16 12:27:54 +02:00
In the case below, the call of `Dispose()` never happens.
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
2023-06-16 12:27:54 +02:00
var a = false;
if (a)
{
Dispose(); // Never reached
}
----
=== Exceptions
This rule will not raise an issue in either of these cases:
* When the condition is a single `const bool`
+
[source,csharp]
----
const bool debug = false;
//...
if (debug)
{
// Print something
}
----
* When the condition is the literal `true` or `false`.
In these cases, it is obvious the code is as intended.
== How to fix it
The conditions should be reviewed to decide whether:
* to update the condition or
* to remove the condition.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
2020-06-30 12:48:07 +02:00
public void Sample(bool b)
{
bool a = false;
2023-06-16 12:27:54 +02:00
if (a) // Noncompliant: The true branch is never reached
2020-06-30 12:48:07 +02:00
{
2023-06-16 12:27:54 +02:00
DoSomething(); // Never reached
2020-06-30 12:48:07 +02:00
}
2023-06-16 12:27:54 +02:00
if (!a || b) // Noncompliant: "!a" is always "true" and the false branch is never reached
2020-06-30 12:48:07 +02:00
{
DoSomething();
}
else
{
2023-06-16 12:27:54 +02:00
DoSomethingElse(); // Never reached
2020-06-30 12:48:07 +02:00
}
2023-06-16 12:27:54 +02:00
var c = "xxx";
2023-08-11 09:56:44 +02:00
var res = c ?? "value"; // Noncompliant: c is always not null, "value" is never used
2020-06-30 12:48:07 +02:00
}
----
2023-06-16 12:27:54 +02:00
==== Compliant solution
2020-06-30 12:48:07 +02:00
2023-06-16 12:27:54 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2020-06-30 12:48:07 +02:00
----
public void Sample(bool b)
{
bool a = false;
2023-06-16 12:27:54 +02:00
if (Foo(a)) // Condition was updated
2020-06-30 12:48:07 +02:00
{
DoSomething();
}
2023-06-16 12:27:54 +02:00
if (b) // Parts of the condition were removed.
2020-06-30 12:48:07 +02:00
{
DoSomething();
}
else
{
DoSomethingElse();
}
2023-06-16 12:27:54 +02:00
var c = "xxx";
var res = c; // ?? "value" was removed
2020-06-30 12:48:07 +02:00
}
----
2023-06-16 12:27:54 +02:00
include::../see.adoc[]
2020-06-30 12:48:07 +02:00
2023-06-16 12:27:54 +02:00
=== Documentation
2020-06-30 12:48:07 +02:00
2023-06-16 12:27:54 +02:00
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators#conditional-logical-and-operator-[Conditional logical AND operator &&]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators#conditional-logical-or-operator-[Conditional logical OR operator ||]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator[?? and ??= operators - the null-coalescing operators]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
2023-08-11 09:56:44 +02:00
* Change this condition so that it does not always evaluate to "[true|false]". Some code paths are unreachable.
* Change this expression which always evaluates to the same result. Some code paths are unreachable.
2023-05-25 14:18:12 +02:00
2021-09-20 15:38:42 +02:00
include::../highlighting.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]