Skip to content

Latest commit

 

History

History
31 lines (19 loc) · 1004 Bytes

rule.adoc

File metadata and controls

31 lines (19 loc) · 1004 Bytes

Certain mathematical comparisons will always return the same value, and should simply not be made.

These comparisons will always return false:

  • comparing a byte with an int or long constant that’s outside the byte range

  • comparing an int with a long constant that’s outside the int range

  • comparing a value guaranteed to be negative with a one that’s guaranteed to be non-negative

These will always return true:

  • comparing aByte <= Byte.MAX_VALUE and aByte >= Byte.MIN_VALUE

  • comparing anInt <= Integer.MAX_VALUE and anInt >= Integer.MIN_VALUE

  • comparing aLong <= Long.MAX_VALUE and aLong >= Long.MIN_VALUE

Noncompliant Code Example

byte b = 1;
int i = Integer.MAX_VALUE;
long m = Integer.MAX_VALUE +1;

if (b == i) {...} // Noncompliant
if (i == m) {...} // Noncompliant
if (i +1 == m) {...} // Noncompliant; iPos +1 == Integer.MIN_VALUE