Skip to content

SONARJAVA-6744 Implement new rule S9148: "Float.compare" or "Double.compare" should be used for floating-point comparisons - #5910

Draft
romainbrenguier wants to merge 3 commits into
masterfrom
new-rule/SONARJAVA-6744-S9148
Draft

SONARJAVA-6744 Implement new rule S9148: "Float.compare" or "Double.compare" should be used for floating-point comparisons#5910
romainbrenguier wants to merge 3 commits into
masterfrom
new-rule/SONARJAVA-6744-S9148

Conversation

@romainbrenguier

Copy link
Copy Markdown
Contributor

Detect incorrect floating-point comparisons (subtraction and relational operators on float/double) inside compareTo, Comparator.compare methods, and Comparator lambdas. Users should use Double.compare or Float.compare instead to correctly handle NaN and negative zero.

Detect incorrect floating-point comparisons (subtraction and relational
operators on float/double) inside compareTo, Comparator.compare methods,
and Comparator lambdas. Users should use Double.compare or Float.compare
instead to correctly handle NaN and negative zero.
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

SONARJAVA-6744

romainbrenguier and others added 2 commits August 10, 2026 15:20
… classes

- Add null-check for MethodTree.block() to skip abstract/interface methods
- Restrict isCompareMethod to classes implementing java.util.Comparator
- Fix column indicator in test sample
- Set quickfix to "infeasible" since no quickfixes are implemented
- Add test cases for abstract methods and non-Comparator utility methods

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Merge the identical MINUS and relational branches in
  FloatingPointComparisonVisitor into a single condition (fixes S1871).
- Add the autoscan expected baseline for S9148 (hasTP, no FP/FN), taken
  from the failing CI run's artifact, and the matching entry in
  autoscan-diff-by-rules.json.
- Add test samples covering the previously uncovered paths: nested
  non-Comparator lambda, local class inside compareTo, a single
  floating-point operand, and methods that only look like comparison
  methods. Coverage of the check goes from 85.7% to ~98.7% (all lines
  covered, 43 of 44 conditions).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@gitar-bot

gitar-bot Bot commented Aug 10, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 2 resolved / 2 findings

Implements new rule S9148 to detect incorrect floating-point comparisons inside comparison methods and lambdas, addressing null pointer exceptions on abstract methods and false positives on non-Comparator classes.

✅ 2 resolved
Bug: NPE on abstract compareTo/compare methods

📄 java-checks/src/main/java/org/sonar/java/checks/FloatingPointComparisonCheck.java:46-50
isCompareToMethod/isCompareMethod match purely on name, int return type and parameter count, so an abstract int compareTo(T) or int compare(T,T) declared in an interface or abstract class also matches. MethodTree.block() is @Nullable and returns null for such methods, so methodTree.block().accept(...) throws a NullPointerException, causing the check to fail on any file containing such a declaration. Guard with if (methodTree.block() != null) before visiting.

Bug: 'compare' detection does not require Comparator type

📄 java-checks/src/main/java/org/sonar/java/checks/FloatingPointComparisonCheck.java:65-69
isCompareMethod flags any method named compare that returns int and has two parameters, regardless of whether the enclosing class implements java.util.Comparator. Unrelated helper methods such as int compare(double a, double b) in an arbitrary utility class will produce issues even though they are not part of a Comparator contract, leading to false positives. Consider verifying the enclosing class is a subtype of java.util.Comparator (mirroring the lambda handling) or that the method is an override of Comparator.compare.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant