Skip to content

Commit

Permalink
[Fix rubocop#13051] Fix an error for Lint/FloatComparison
Browse files Browse the repository at this point in the history
Fixes rubocop#13051.

This PR fixes an error for `Lint/FloatComparison`
when comparing with rational literal.

NOTE: Using rubocop/rubocop-ast#304 in the future will allow
for a more fundamental resolution of the issue.
  • Loading branch information
koic committed Jul 16, 2024
1 parent 98c5181 commit 23d4532
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_lint_float_comparison.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13051](https://github.com/rubocop/rubocop/issues/13051): Fix an error for `Lint/FloatComparison` when comparing with rational literal. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/lint/float_comparison.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def float?(node)
end

def literal_zero?(node)
node&.numeric_type? && node.value.zero?
# FIXME: https://github.com/rubocop/rubocop-ast/pull/304 is released,
# replace this condition with `node&.numeric_type? && node.value.zero?`.
node&.numeric_type? && node.node_parts[0].zero?
end

# rubocop:disable Metrics/PerceivedComplexity
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/lint/float_comparison_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
RUBY
end

it 'does not register an offense when comparing with rational literal' do
expect_no_offenses(<<~RUBY)
value == 0.2r
RUBY
end

it 'does not register an offense when comparing against zero' do
expect_no_offenses(<<~RUBY)
x == 0.0
Expand Down

0 comments on commit 23d4532

Please sign in to comment.