rspec/rules/S3949/csharp/rule.adoc

43 lines
840 B
Plaintext
Raw Normal View History

== 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 or assign numbers beyond those bounds, and the result will be a value that has silently wrapped around from the expected positive value to a negative one, or vice versa.
== Noncompliant code example
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:39 +02:00
----
public int Transform(int value)
{
if (value <= 0)
{
return value;
}
int number = int.MaxValue;
return number + value; // Noncompliant
2020-06-30 12:48:39 +02:00
}
----
== Compliant solution
[source,csharp]
----
public long Transform(int value)
{
if (value <= 0)
{
return value;
}
long number = int.MaxValue;
return number + value;
}
----
== Resources
include::../common/resources/standards.adoc[]
include::../rspecator.adoc[]