rspec/rules/S3449/vbnet/rule.adoc
2023-06-07 12:01:33 +02:00

34 lines
1.6 KiB
Plaintext

== Why is this an issue?
Numbers can be shifted with the `<<` and `>>` https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/bit-shift-operators[operators], but the right operand of the operation needs to be an `int` or a type that has an https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/implicit-and-explicit-conversions[implicit conversion] to `int`. However, when the left operand is an `object`, the compiler's type checking is turned off, so you can pass anything to the right of a shift operator and have it compile. And if the argument can't be implicitly converted to `int` at runtime, then a https://learn.microsoft.com/en-us/dotnet/api/microsoft.csharp.runtimebinder.runtimebinderexception[RuntimeBinderException] will be raised.
[source,vbnet]
----
Dim o As Object = 5
Dim x As Integer = 5
x = o >> 5 ' Noncompliant
x = x << o ' Noncompliant
----
=== Exceptions
This rule does not raise when the left or the right expression is https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing[Nothing].
[source,vbnet]
----
x = Nothing >> 5
x = 5 << Nothing
----
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/bit-shift-operators[Bit Shift Operators]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/implicit-and-explicit-conversions[Implicit and Explicit Conversions]
* https://learn.microsoft.com/en-us/dotnet/api/microsoft.csharp.runtimebinder.runtimebinderexception[RuntimeBinderException Class]
include::../rspecator-dotnet.adoc[]