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

[Fix #12146] Fix a false positive for Lint/FloatComparison #12387

Merged
merged 1 commit into from Nov 29, 2023
Merged

[Fix #12146] Fix a false positive for Lint/FloatComparison #12387

merged 1 commit into from Nov 29, 2023

Conversation

Earlopain
Copy link
Contributor

@Earlopain Earlopain commented Nov 14, 2023

Allow comparing against 0. See #12387 (comment)


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic
Copy link
Member

koic commented Nov 20, 2023

This cop is categorized under Lint, not Style. 0.zero?, 0.0.zero?, and 0 == 0.0 all return true. Therefore, it doesn't seem like bad code that should be marked as bad in Lint.

It appears that this issue should rather be addressed under Style/NumericPredicate to ensure consistency, I think.

@Earlopain
Copy link
Contributor Author

Earlopain commented Nov 20, 2023

0.0 == 0.0 returns true as well but is still flagged by Lint/FloatComparison. That is kind of the point that I made in my initial issue. 0 has special meaning and sometimes it is wanted to check for it. Basically #8432 (comment)

My understanding is that Style/NumericPredicate enforces uniform usage of x == 0 and x.zero? while Lint/FloatComparison makes you aware that you are dealing with floating point numbers and should be careful. This change seems appropriate, at least for how it currently works.

However, if you want to change Lint/FloatComparison to not flag comparision by zero, especially with x.to_f == 0.0 which is currently an offense, I can give that a shot as well.

@koic
Copy link
Member

koic commented Nov 20, 2023

0.0 == 0.0 returns true as well but is still flagged by Lint/FloatComparison.

This looks like a false positive. In any case, registering an offense for zero? by Lint cop seems strict for the users.

However, if you want to change Lint/FloatComparison to not flag comparision by zero, especially with x.to_f == 0.0 which is currently an offense, I can give that a shot as well.

Yeah, that seems reasonable to me. I think zero is special.

@Earlopain
Copy link
Contributor Author

Sure thing, I'll change it over then. Marking this as draft for the meanwhile.

@Earlopain Earlopain marked this pull request as draft November 20, 2023 09:41
@Earlopain Earlopain changed the title [Fix #12146] Fix a false negative for Lint/FloatComparison [Fix #12146] Fix a false positive for Lint/FloatComparison Nov 20, 2023
@Earlopain Earlopain marked this pull request as ready for review November 20, 2023 12:33
@Earlopain
Copy link
Contributor Author

Changed as requested. I'm not entirely sure if I only need to check in on_send but I couldn't think of an example that doesn't work with this

@koic koic merged commit a31a37c into rubocop:master Nov 29, 2023
28 checks passed
@koic
Copy link
Member

koic commented Nov 29, 2023

Thanks!

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.

None yet

2 participants