114 lines
2.8 KiB
Plaintext
114 lines
2.8 KiB
Plaintext
== Why is this an issue?
|
|
|
|
include::../description.adoc[]
|
|
|
|
In the case below, the call of `Dispose()` never happens.
|
|
|
|
[source,vbnet]
|
|
----
|
|
Dim a = False
|
|
|
|
If a Then
|
|
Dispose() ' Never reached
|
|
End If
|
|
----
|
|
|
|
=== 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.
|
|
|
|
== 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,vbnet,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
Public Sub Sample(ByVal b As Boolean)
|
|
Dim a = False
|
|
If a Then ' Noncompliant: The true branch is never reached
|
|
DoSomething() ' Never reached
|
|
End If
|
|
|
|
If Not a OrElse b Then ' Noncompliant: "not a" is always "True" and the false branch is never reached
|
|
DoSomething()
|
|
Else
|
|
DoSomethingElse() ' Never reached
|
|
End If
|
|
|
|
Dim c = "xxx"
|
|
Dim res = If(c, "value") ' Noncompliant: d is always not Nothing, "value" is never used
|
|
End Sub
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,vbnet,diff-id=1,diff-type=compliant]
|
|
----
|
|
Public Sub Sample(ByVal b As Boolean)
|
|
Dim a = False
|
|
If Foo(a) Then ' Condition was updated
|
|
DoSomething()
|
|
End If
|
|
|
|
If b Then ' Parts of the condition were removed.
|
|
DoSomething()
|
|
Else
|
|
DoSomethingElse()
|
|
End If
|
|
|
|
Dim c = "xxx"
|
|
Dim res = c ' "value" was removed
|
|
End Sub
|
|
----
|
|
|
|
include::../see.adoc[]
|
|
|
|
=== Documentation
|
|
|
|
* 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/if-operator#if-operator-called-with-two-arguments[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]". Some code paths are unreachable.
|
|
* Change this expression which always evaluates to the same result. Some code paths are unreachable.
|
|
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|