rspec/rules/S1751/vbnet/rule.adoc
2023-07-06 14:31:26 +02:00

67 lines
3.0 KiB
Plaintext

== Why is this an issue?
A https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/control-flow/loop-structures[loop] statement with at most one iteration is equivalent to an `If` statement; the following block is executed only once.
If the initial intention was to conditionally execute the block only once, an `If` statement should be used instead. If that was not the initial intention, the block of the loop should be fixed so the block is executed multiple times.
A loop statement with at most one iteration can happen when a statement unconditionally transfers control, such as a jump statement or a throw statement, is misplaced inside the loop block.
This rule raises when the following statements are misplaced:
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/exit-statement[`Exit`]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/continue-statement[`Continue`]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/return-statement[`Return`]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/throw-statement[`Throw`]
== How to fix it
=== Code examples
==== Noncompliant code example
[source,vbnet,diff-id=1,diff-type=noncompliant]
----
Public Function Method(items As IEnumerable(Of Object)) As Object
For i As Integer = 0 To 9
Console.WriteLine(i)
Exit For ' Noncompliant: loop only executes once
Next
For Each item As Object In items
Return item ' Noncompliant: loop only executes once
Next
Return Nothing
End Function
----
==== Compliant solution
[source,vbnet,diff-id=1,diff-type=compliant]
----
Public Function Method(items As IEnumerable(Of Object)) As Object
For i As Integer = 0 To 9
Console.WriteLine(i)
Next
Dim item = items.FirstOrDefault()
If item IsNot Nothing Then
Return item
End If
Return Nothing
End Function
----
== Resources
=== Documentation
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/control-flow/loop-structures[Loop Structures (Visual Basic)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/exit-statement[`Exit` Statement (Visual Basic)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/continue-statement[`Continue` Statement (Visual Basic)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/return-statement[`Return` Statement (Visual Basic)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/throw-statement[`Throw` Statement (Visual Basic)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/throw-statement[Throw Statement (Visual Basic)]
include::../rspecator-dotnet.adoc[]