SONARJAVA-6744 Implement new rule S9148: "Float.compare" or "Double.compare" should be used for floating-point comparisons - #5910
Conversation
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.
… 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>
Code Review ✅ Approved 2 resolved / 2 findingsImplements 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
✅ Bug: 'compare' detection does not require Comparator type
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




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.