rspec/rules/S1862/vbnet/rule.adoc

63 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2023-06-28 13:52:34 +02:00
A chain of https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/if-then-else-statement[If/ElseIf] statements is evaluated from top to bottom. At most, only one branch will be executed: the first statement with a condition that evaluates to `True`.
Therefore, duplicating a condition leads to unreachable code inside the duplicated condition block. Usually, this is due to a copy/paste error.
2020-06-30 12:47:33 +02:00
2023-06-28 13:52:34 +02:00
The result of such duplication can lead to unreachable code or even to unexpected behavior.
2021-02-02 15:02:10 +01:00
2023-06-28 13:52:34 +02:00
== How to fix it
2020-06-30 12:47:33 +02:00
2023-06-28 13:52:34 +02:00
=== Code examples
2020-06-30 12:47:33 +02:00
2023-06-28 13:52:34 +02:00
==== Noncompliant code example
[source,vbnet,diff-id=1,diff-type=noncompliant]
2020-06-30 12:47:33 +02:00
----
If param = 1 Then
OpenWindow()
ElseIf param = 2 Then
CloseWindow()
2023-06-28 13:52:34 +02:00
ElseIf param = 1 Then ' Noncompliant: condition has already been checked
MoveWindowToTheBackground() ' unreachable code
2020-06-30 12:47:33 +02:00
End If
----
2023-06-28 13:52:34 +02:00
==== Compliant solution
2020-06-30 12:47:33 +02:00
2023-06-28 13:52:34 +02:00
[source,vbnet,diff-id=1,diff-type=compliant]
2020-06-30 12:47:33 +02:00
----
If param = 1 Then
OpenWindow()
ElseIf param = 2 Then
CloseWindow()
ElseIf param = 3 Then
MoveWindowToTheBackground()
End If
----
2023-06-28 13:52:34 +02:00
== Resources
=== Documentation
2023-06-28 13:52:34 +02:00
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/if-then-else-statement[If...Then...Else Statement]
ifdef::env-github,rspecator-view,env-vscode[]
'''
== Implementation Specification
(visible only on this page)
2023-06-28 13:52:34 +02:00
=== Message
This branch duplicates the one on line n.
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-28 13:52:34 +02:00
endif::env-github,rspecator-view,env-vscode[]