rspec/rules/S3949/vbnet/rule.adoc

28 lines
911 B
Plaintext

== Why is this an issue?
Numbers are infinite, but the types that hold them are not. Each numeric type has hard upper and lower bounds. Try to calculate numbers beyond those bounds, and the result will be an `OverflowException`. When the compilation is configured to remove integer overflow checking, the value will be silently wrapped around from the expected positive value to a negative one, or vice versa.
== Noncompliant code example
[source,vbnet]
----
Public Function Transform(Value As Integer) As Integer
If Value <= 0 Then Return Value
Dim Number As Integer = Integer.MaxValue
Return Number + Value ' Noncompliant
End Function
----
== Compliant solution
[source,vbnet]
----
Public Function Transform(Value As Integer) As Long
If Value <= 0 Then Return Value
Dim Number As Long = Integer.MaxValue
Return Number + Value
End Function
----
include::../rspecator.adoc[]