From 38ffd02fc370dbc1030d06495f8fdbfe3be27a94 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 18 Dec 2024 16:32:34 +0100 Subject: [PATCH] Create rule S6146: "Option Explicit" should be enabled (#4582) * Add vb6 to rule S6146 * Add description * Update description for LaYC --------- Co-authored-by: thahnen Co-authored-by: Tobi Hahnen --- rules/S6146/vb6/metadata.json | 2 ++ rules/S6146/vb6/rule.adoc | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 rules/S6146/vb6/metadata.json create mode 100644 rules/S6146/vb6/rule.adoc diff --git a/rules/S6146/vb6/metadata.json b/rules/S6146/vb6/metadata.json new file mode 100644 index 0000000000..7a73a41bfd --- /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 0000000000..a3828481b4 --- /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 +----