org.sonar.l10n.java.rules.squid.S2197.html Maven / Gradle / Ivy
The newest version!
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 false negatives.
Noncompliant Code Example
public boolean isOdd(int x) {
return x % 2 == 1; // Noncompliant; if x is negative, x % 2 == -1
}
Compliant Solution
public boolean isOdd(int x) {
return x %2 != 0;
}
or
public boolean isOdd(int x) {
return Math.abs(x %2) != 1;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy