Create rule S6146: "Option Explicit" should be enabled (#4582)
* Add vb6 to rule S6146 * Add description * Update description for LaYC --------- Co-authored-by: thahnen <thahnen@users.noreply.github.com> Co-authored-by: Tobi Hahnen <tobias.hahnen@sonarsource.com>
This commit is contained in:
parent
1d97909d90
commit
38ffd02fc3
2
rules/S6146/vb6/metadata.json
Normal file
2
rules/S6146/vb6/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
36
rules/S6146/vb6/rule.adoc
Normal file
36
rules/S6146/vb6/rule.adoc
Normal file
@ -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
|
||||||
|
----
|
Loading…
x
Reference in New Issue
Block a user