CodeQL documentation

Inconsistent compareTo

ID: java/inconsistent-compareto-and-equals
Kind: problem
Severity: warning
Precision: medium
Tags:
   - reliability
   - correctness
Query suites:
   - java-security-and-quality.qls

Click to see the query in the CodeQL repository

A class that overrides compareTo but not equals may not implement a natural ordering that is consistent with equals.

Recommendation

Although this consistency is not strictly required by the compareTo contract, usually both methods should be overridden to ensure that they are consistent, that is, that x.compareTo(y)==0 is true if and only if x.equals(y) is true, for any non-null x and y.

Example

In the following example, the class InconsistentCompareTo overrides compareTo but not equals.

public class InconsistentCompareTo implements Comparable<InconsistentCompareTo> {
	private int i = 0;
	public InconsistentCompareTo(int i) {
		this.i = i;
	}
	
	public int compareTo(InconsistentCompareTo rhs) {
		return i - rhs.i;
	}
}

In the following example, the class InconsistentCompareToFix overrides both compareTo and equals.

public class InconsistentCompareToFix implements Comparable<InconsistentCompareToFix> {
	private int i = 0;
	public InconsistentCompareToFix(int i) {
		this.i = i;
	}
	
	public int compareTo(InconsistentCompareToFix rhs) {
		return i - rhs.i;
	}

	public boolean equals(InconsistentCompareToFix rhs) {
		return i == rhs.i;
	}
}

If you require a natural ordering that is inconsistent with equals, you should document it clearly.

References