Standard Analyses

SemmleCode Professional comes with a wide range of pre-packaged analyses, from architectural properties and metrics, to statement-level checks for likely bugs and violations of best practice.


Suspicious Reference Equality Test of Boxed Types

This query finds code that compares two boxed primitive values using == or !=. These operators compare object identity, which may not be intended.

For example, the method

    boolean m(Integer i, Integer j) {
        return i == j;
    }

returns false when invoked as

    m(23, 23)
How to Interpret the Query Results

The query flags all occurrences of such an equality test and provides a list of all found occurrences in the results view.

How to Address the Query Results

Usually, non-primitive objects (such as boxed primitive types) should be compared using their equals methods.

Source Code
import default

from EqualityTest c, Expr lhs, Expr rhs
where c.getLeftOperand().getType() instanceof BoxedType and
      c.getRightOperand().getType() instanceof BoxedType
select c, "Suspicious reference comparison of boxed numerical values"
References