2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-16 09:28:19 +02:00
An operand of a boolean expression that never changes the result of the expression might not match the programmer's intent and can lead to unexpected behavior and potential bugs.
2020-06-30 12:48:07 +02:00
2023-06-16 09:28:19 +02:00
[source,csharp]
----
var a = true;
if (a)
{
DoSomething();
}
----
This also applies to the https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator[null coalescing operator] when one of the operands always evaluates to `null`.
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 09:28:19 +02:00
string d = null;
var v1 = d ?? "value";
----
=== 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 unnecessary operand
* to remove the unnecessary operand
=== Code examples
==== Noncompliant code example
2023-08-16 10:45:12 +02:00
[source,csharp]
2023-06-16 09:28:19 +02:00
----
public void Sample(bool b, bool c)
2020-06-30 12:48:07 +02:00
{
var a = true;
2023-06-16 09:28:19 +02:00
if (a) // Noncompliant: "a" is always "true"
2020-06-30 12:48:07 +02:00
{
DoSomething();
}
2023-06-16 09:28:19 +02:00
if (b && a) // Noncompliant: "a" is always "true"
2020-06-30 12:48:07 +02:00
{
DoSomething();
}
2023-06-16 09:28:19 +02:00
if (c || !a) // Noncompliant: "!a" is always "false"
2020-06-30 12:48:07 +02:00
{
DoSomething();
}
string d = null;
2023-06-16 09:28:19 +02:00
var v1 = d ?? "value"; // Noncompliant: "d" is always null and v1 is always "value".
var v2 = s ?? d; // Noncompliant: "d" is always null and v2 is always equal to s.
2020-06-30 12:48:07 +02:00
}
----
2023-06-16 09:28:19 +02:00
==== Compliant solution
2020-06-30 12:48:07 +02:00
2023-06-16 09:28:19 +02:00
The unnecessary operand is updated:
2023-08-16 10:45:12 +02:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
public void Sample(bool b, bool c, string s)
{
2023-06-16 09:28:19 +02:00
var a = IsAllowed();
if (a) // Compliant
{
DoSomething();
}
if (b && a) // Compliant
{
DoSomething();
}
if (c || !a) // Compliant
2020-06-30 12:48:07 +02:00
{
DoSomething();
}
2023-06-16 09:28:19 +02:00
string d = GetStringData();
var v1 = d ?? "value"; // Compliant
var v2 = s ?? d; // Compliant
}
----
The unnecessary operand is removed:
2023-08-16 10:45:12 +02:00
[source,csharp]
2023-06-16 09:28:19 +02:00
----
public void Sample(bool b, bool c, string s)
{
DoSomething();
if (b) // Compliant
2020-06-30 12:48:07 +02:00
{
DoSomething();
}
2023-06-16 09:28:19 +02:00
if (c) // Compliant
2020-06-30 12:48:07 +02:00
{
DoSomething();
}
2023-06-16 09:28:19 +02:00
var v1 = "value"; // Compliant
var v2 = s; // Compliant
2020-06-30 12:48:07 +02:00
}
----
2023-06-16 09:28:19 +02:00
== Resources
=== Documentation
* https://cwe.mitre.org/data/definitions/571[MITRE, CWE-571] - Expression is Always True
* https://cwe.mitre.org/data/definitions/570[MITRE, CWE-570] - Expression is Always False
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators#conditional-logical-and-operator-[Conditional logical AND operator &&]
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators#conditional-logical-or-operator-[Conditional logical OR operator ||]
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator[Null-coalescing operators ?? and ??=]
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
* Change this condition so that it does not always evaluate to "[true|false]".
2023-08-11 09:57:19 +02:00
* Change this expression which always evaluates to the same result.
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[]