-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S6146: "Option Explicit" should be enabled (#4582)
* 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
1 parent
1d97909
commit 38ffd02
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
---- |