Skip to content

Commit

Permalink
Create rule S6146: "Option Explicit" should be enabled (#4582)
Browse files Browse the repository at this point in the history
* Add vb6 to rule S6146

* Add description

* Update description for LaYC

---------

Co-authored-by: thahnen <[email protected]>
Co-authored-by: Tobi Hahnen <[email protected]>
  • Loading branch information
3 people authored Dec 18, 2024
1 parent 1d97909 commit 38ffd02
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rules/S6146/vb6/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
36 changes: 36 additions & 0 deletions rules/S6146/vb6/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
== Why is this an issue?

There are several compilations options available for Visual Basic source code and `Option Explicit` defines compiler behavior for implicit variable declarations. Not specifying `Option Explicit` will allow creating a variable by it's first usage. This behavior can lead to unexpected runtime errors due to typos in variable names.

== How to fix it

`Option Explicit` should be added to every individual source file.

=== Code examples

==== Noncompliant code example

[source,vb6,diff-id=1,diff-type=noncompliant]
----
Sub DoSomething(First As String, Second As String)
Parameter = Fist ' New local variable "Fist" is created and assigned to new local variable "Parameter" instead of "First" argument.
DoSomething(Parameter)
Parametr = Second ' "Second" argument is assigned to newly created variable "Parametr" instead of intended "Parameter".
DoSomething(Parameter) ' Value of "Parameter" is always Nothing
End Sub
----


==== Compliant solution

[source,vb6,diff-id=1,diff-type=compliant]
----
Option Explicit
Sub DoSomething(First As String, Second As String)
Dim Parameter As String = First
DoSomething(Parameter)
Parameter = Second
DoSomething(Parameter)
End Sub
----

0 comments on commit 38ffd02

Please sign in to comment.