rspec/rules/S6145/rule.adoc

65 lines
1.5 KiB
Plaintext

== Why is this an issue?
There are several compilations options available for Visual Basic source code and ``++Option Strict++`` defines compiler behavior for implicit data type conversions. Specifying ``++Option Strict Off++`` will allow:
* Implicit narrowing conversions
* Late binding
* Implicit typing that results in an ``++Object++`` type
This behavior can lead to unexpected runtime errors due to type mismatch or missing members.
``++Option Strict++`` can be set in project properties or overridden in individual source files.
=== Noncompliant code example
[source,text]
----
Option Strict Off ' Noncompliant
Public Class KnownType
Public ReadOnly Property Name As String
End Class
Public Module MainMod
Public Function DoSomething(Arg) As String ' Type for "Arg" argument is not defined.
Dim Item As KnownType = Arg ' Implicit narrowing conversion doesn't enforce "Arg" to be of type "KnownType"
Return Arg.Title ' "Title" might not exist in "Arg"
End Function
End Module
----
=== Compliant solution
[source,text]
----
Option Strict On
Public Class KnownType
Public ReadOnly Property Name As String
End Class
Public Module MainMod
Public Function DoSomething(Arg As KnownType) As String
Dim Item As KnownType = Arg
Return Arg.Name
End Function
End Module
----
== Resources
* https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement[Visual Basic documentation - Option Strict Statement]