diff --git a/rules/S6146/vb6/metadata.json b/rules/S6146/vb6/metadata.json new file mode 100644 index 00000000000..7a73a41bfdf --- /dev/null +++ b/rules/S6146/vb6/metadata.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/rules/S6146/vb6/rule.adoc b/rules/S6146/vb6/rule.adoc new file mode 100644 index 00000000000..a3828481b42 --- /dev/null +++ b/rules/S6146/vb6/rule.adoc @@ -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 +----