Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint/FloatComparison, Style/NumericPredicate and comparing by 0 #12146

Closed
Earlopain opened this issue Aug 22, 2023 · 0 comments
Closed

Lint/FloatComparison, Style/NumericPredicate and comparing by 0 #12146

Earlopain opened this issue Aug 22, 2023 · 0 comments

Comments

@Earlopain
Copy link
Contributor

I have the following rubocop config:

Style/NumericPredicate:
  EnforcedStyle: comparison

Lint/FloatComparison discourages comparing by absolute values. I know why but zero seems like something that should be made an exception for.

if value.to_f != 0.0 # Lint/FloatComparison: Avoid (in)equality comparisons of floats as they are unreliable
  10 / value.to_f
end

If I switch to EnforcedStyle: predicate for Style/NumericPredicate I can write the following without offence:

unless value.to_f.zero? # All good!
  10 / value.to_f
end

value is user-provided and I want to prevent NaN from being returned, falling back to some other logic in that case. Alternatively, how would I write this without triggering an offense?

Expected behavior

Rubocop should either allow value.to_f != 0.0 or disallow value.to_f.zero?

RuboCop version

$ [bundle exec] rubocop -V
1.55.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.2.2) [x86_64-linux-musl]
  - rubocop-erb 0.3.0
  - rubocop-rails 2.20.2
@koic koic closed this as completed in 7a66f24 Nov 29, 2023
koic added a commit that referenced this issue Nov 29, 2023
…mparison

[Fix #12146] Fix a false positive for `Lint/FloatComparison`
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

No branches or pull requests

1 participant