All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S2197.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

Why is this an issue?

When the modulus of a negative number is calculated, the result will either be negative or zero. Thus, comparing the modulus of a variable for equality with a positive number (or a negative one) could result in unexpected results.

Noncompliant code example

public bool IsOdd(int x)
{
  return x % 2 == 1;  // Noncompliant; if x is an odd negative, x % 2 == -1
}

Compliant solution

public bool IsOdd(int x)
{
  return x % 2 != 0;
}

or

public bool IsOdd(uint x)
{
  return x % 2 == 1;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy