include::../introduction.adoc[]

include::../why-is-this-an-issue.adoc[]

include::../what-is-the-potential-impact.adoc[]

This rule looks for operands of a boolean expression never changing the result of the expression. It also applies to the https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/null-conditional-operators[null conditional operator] when one of the operands always evaluates to `Nothing`.

[source,vbnet]
----
Dim d As String = Nothing
Dim v1 = If(d, "value")
----

=== Exceptions

This rule will not raise an issue in either of these cases:

* When the condition is a single `Const bool`

[source,vbnet]
----
Const debug = False
'...
If debug Then
    ' Print something
End If
----
* When the condition is the literal `True` or `False`.

In these cases, it is obvious the code is as intended.

include::../how-to-fix-it.adoc[]

=== Code examples

==== Noncompliant code example

[source,vbnet,diff-id=1,diff-type=noncompliant]
----
Public Sub Sample(ByVal b As Boolean, ByVal c As Boolean)
    Dim a = True
    If a Then                  ' Noncompliant: "a" is always "true"
        DoSomething()
    End If

    If b AndAlso a Then        ' Noncompliant: "a" is always "true"
        DoSomething()
    End If

    If c OrElse Not a Then     ' Noncompliant: "Not a" is always "false"
        DoSomething()
    End If

    Dim d As String = Nothing
    Dim v1 = If(d, "value")    ' Noncompliant: "d" is always Nothing and v1 is always "value".
    Dim v2 = If(s, d)          ' Noncompliant: "d" is always Nothing and v2 is always equal to s.
End Sub
----

==== Compliant solution

[source,vbnet,diff-id=1,diff-type=compliant]
----
Public Sub Sample(ByVal b As Boolean, ByVal c As Boolean, ByVal s As String)
    Dim a = IsAllowed()
    If a Then                   ' Compliant
        DoSomething()
    End If

    If b AndAlso a Then         ' Compliant
        DoSomething()
    End If

    If c OrElse Not a Then      ' Compliant
        DoSomething()
    End If

    Dim d As String = GetStringData()
    Dim v1 = If(d, "value")     ' Compliant
    Dim v2 = If(s, d)           ' Compliant
End Sub
----

== Resources

=== Documentation

* CWE - https://cwe.mitre.org/data/definitions/571[CWE-571 - Expression is Always True]
* CWE - https://cwe.mitre.org/data/definitions/570[CWE-570 - Expression is Always False]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/operators-and-expressions/logical-and-bitwise-operators[Logical and Bitwise Operators in Visual Basic]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/null-conditional-operators[?. and ?() null-conditional operators (Visual Basic)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/null-conditional-operators[If operator called with two arguments]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

* Change this condition so that it does not always evaluate to "[true|false]".
* Change this expression which always evaluates to the same result.


include::../highlighting.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
