rspec/rules/S6145/rule.adoc

65 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
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:
2021-01-26 04:07:35 +00:00
* Implicit narrowing conversions
* Late binding
2021-01-27 13:42:22 +01:00
* Implicit typing that results in an ``++Object++`` type
2021-01-26 04:07:35 +00:00
This behavior can lead to unexpected runtime errors due to type mismatch or missing members.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
``++Option Strict++`` can be set in project properties or overridden in individual source files.
2021-01-26 04:07:35 +00:00
=== Noncompliant code example
2021-01-26 04:07:35 +00:00
2022-02-04 17:28:24 +01:00
[source,text]
2021-01-26 04:07:35 +00:00
----
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
2021-01-26 04:07:35 +00:00
2022-02-04 17:28:24 +01:00
[source,text]
2021-01-26 04:07:35 +00:00
----
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
2021-01-26 04:07:35 +00:00
* https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement[Visual Basic documentation - Option Strict Statement]
2021-01-26 04:07:35 +00:00