rspec/rules/S3449/vbnet/rule.adoc

34 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2023-06-06 16:48:28 +02:00
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.
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,vbnet]
2020-06-30 12:48:39 +02:00
----
Dim o As Object = 5
Dim x As Integer = 5
x = o >> 5 ' Noncompliant
2023-06-06 16:48:28 +02:00
x = x << o ' Noncompliant
2020-06-30 12:48:39 +02:00
----
=== Exceptions
2020-06-30 12:48:39 +02:00
2023-06-06 16:48:28 +02:00
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]
2020-06-30 12:48:39 +02:00
----
x = Nothing >> 5
2023-06-06 16:48:28 +02:00
x = 5 << Nothing
2020-06-30 12:48:39 +02:00
----
2023-06-06 16:48:28 +02:00
== Resources
2023-06-06 16:48:28 +02:00
=== Documentation
2023-06-06 16:48:28 +02:00
* 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]
2023-06-06 16:48:28 +02:00
include::../rspecator-dotnet.adoc[]