Avoid direct float comparisons with 0.0 in storage.rb #20871
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently the rubocop linter gives us a handful of warnings for the Storage class like this:
app/models/storage.rb:621:5: W: Lint/FloatComparison: Avoid (in)equality comparisons of floats as they are unreliable. used_space.to_f == 0.0 ? 0.0 : (debris_size.to_f / used_space.to_f * 1000.0).round / 10.0
So, this PR just switches them to use the
zero?
method. Core Ruby appears to have its own code for checking floats vs. zero (which the zero? method uses):https://github.com/ruby/ruby/blob/master/numeric.c#L1122-L1126
If we feel this isn't worthwhile, then I would vote to update our rubocop config to exclude these.
Update: originally I was only fixing the linter warnings, but I also went ahead and fixed the
Style/FloatDivision
warnings as well while I was here.