Skip to content

NET-1118 S3385: Remove Exit For, Do, While and Try #4654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions rules/S3385/vbnet/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
== Why is this an issue?

Other than ``++Exit Select++``, using an ``++Exit++`` statement is never a good idea.
`Exit Function`, `Exit Property`, and `Exit Sub` are all poor, less-readable substitutes for a simple `Return`, and if used with code that should return a value (`Exit Function` and in some cases `Exit Property`) they could result in a `NullReferenceException`.


``++Exit Do++``, ``++Exit For++``, ``++Exit Try++``, and ``++Exit While++`` will all result in unstructured control flow, i.e. spaghetti code.


``++Exit Function++``, ``++Exit Property++``, and ``++Exit Sub++`` are all poor, less-readable substitutes for a simple ``++return++``, and if used with code that should return a value (``++Exit Function++`` and in some cases ``++Exit Property++``) they could result in a ``++NullReferenceException++``.


This rule raises an issue for all uses of ``++Exit++`` except ``++Exit Select++`` and ``++Exit Do++`` statements in loops without condition.
This rule raises an issue for all their usages.


=== Noncompliant code example

[source,vbnet]
----
Public Class Sample
Dim condition As Boolean

Public Sub MySub()
If condition Then
Exit Sub ' Noncompliant
End If

For index = 1 To 10
If index = 5 Then
Exit For ' Noncompliant
End If
' ...
Next
Public Sub MySub(Condition As Boolean)
If Condition Then Exit Sub ' Noncompliant
' ...
End Sub
Function MyFunction() As Object

Public Function MyFunction(Condition As Boolean) As Integer
If Condition Then
MyFunction = 42
Exit Function ' Noncompliant
End If
' ...
MyFunction = 42
Exit Function ' Noncompliant
End Function

End Class
----

Expand All @@ -45,21 +33,17 @@ End Class
[source,vbnet]
----
Public Class Sample
Dim condition As Boolean

Public Sub MySub()
If condition Then
Return
End If

For index = 1 To 4
' ...
Next
Public Sub MySub(Condition As Boolean)
If Condition Then Return ' Noncompliant
' ...
End Sub
Function MyFunction() As Object

Public Function MyFunction(Condition As Boolean) As Integer
If Condition Then Return 42
' ...
Return 42
End Function

End Class
----

Expand Down
Loading