Repository navigation
Java: Detect Objects.equals calls on arrays - #23169
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The PR description lacks DCA results for the final query change, including an assessment of changed results and any unexpected regressions.
1 open issue
What changed in this PR
Extends java/equals-on-arrays to detect identity comparisons through java.util.Objects.equals when both arguments have compatible array types. This addresses the helper-API case in #21547; arrays widened to Object remain outside this change.
Changes:
- Adds detection for compatible array arguments to
Objects.equals. - Adds five positive and nine negative test cases with updated expected results.
- Updates query help and adds a release note.
| File | Description |
|---|---|
java/ql/test/query-tests/EqualsArray/ObjectsEquals.java |
Adds positive and negative helper-API test cases. |
java/ql/test/query-tests/EqualsArray/EqualsArray.expected |
Records five new expected alerts. |
java/ql/src/Likely Bugs/Comparison/EqualsArray.ql |
Detects Objects.equals calls with compatible arrays. |
java/ql/src/Likely Bugs/Comparison/EqualsArray.qhelp |
Documents array identity behavior in Objects.equals. |
java/ql/src/change-notes/2026-11-19-objects-equals-arrays.md |
Announces the expanded query coverage. |
Review effort: Balanced (adaptive)
| m.hasQualifiedName("java.util", "Objects", "equals") and | ||
| recvtype = ma.getArgument(0).getType() and | ||
| haveIntersection(recvtype, ma.getArgument(1).getType().(Array)) |
|
QHelp previews: java/ql/src/Likely Bugs/Comparison/EqualsArray.qhelpEquals or hashCode on arraysThe RecommendationTo compare the lengths of the arrays and the corresponding pairs of elements in the arrays, use one of the comparison methods from
ExampleIn the following example, the two arrays are first compared using the public void arrayExample(){
String[] array1 = new String[]{"a", "b", "c"};
String[] array2 = new String[]{"a", "b", "c"};
// Reference equality tested: prints 'false'
System.out.println(array1.equals(array2));
// Equality of array elements tested: prints 'true'
System.out.println(Arrays.equals(array1, array2));
}References
|
aschackmull
left a comment
There was a problem hiding this comment.
Thank you for the contribution. I'll run it through our internal test suite to check for any mis-optimisations, just in case. But otherwise this LGTM.

Extend
java/equals-on-arraysto reportjava.util.Objects.equalscalls with compatible array arguments. These calls compare object identity, so arrays with equal contents can compare unequal.Addresses the
Objects.equalscase in #21547.Validation with CodeQL CLI 2.27.4 passed the EqualsArray query tests, including five new positive cases and nine negative cases. QL formatting, query-help generation, query-ID and repository consistency checks also passed.