Skip to content

Fix Style/FloatDivision cop error on implicit receiver #13642

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

Conversation

viralpraxis
Copy link
Contributor

Cop Style/FloatDivision fails on implicit #to_f receiver.

With single_coerce style:

echo 'a.to_f / to_f' | rubocop --only Style/FloatDivision -c <(echo 'Style/FloatDivision:\n EnforcedStyle: single_coerce') --stdin bug.rb -d

An error occurred while Style/FloatDivision cop was inspecting bug.rb:1:0.
Expected a Parser::Source::Range, Comment or RuboCop::AST::Node, got NilClass
lib/rubocop/cop/corrector.rb:111:in `to_range'
lib/rubocop/cop/corrector.rb:120:in `check_range_validity'
lib/parser/source/tree_rewriter.rb:398:in `combine'
lib/parser/source/tree_rewriter.rb:194:in `replace'
lib/parser/source/tree_rewriter.rb:218:in `remove'
lib/rubocop/cop/style/float_division.rb:126:in `remove_to_f_method'
lib/rubocop/cop/style/float_division.rb:90:in `block in on_send'

With left_coerce or right_coerce or fdiv:

echo 'a / to_f' | rubocop --only Style/FloatDivision -c <(echo 'Style/FloatDivision:\n EnforcedStyle: left_coerce') --stdin bug.rb -d

An error occurred while Style/FloatDivision cop was inspecting bug.rb:1:0.
Expected a Parser::Source::Range, Comment or RuboCop::AST::Node, got NilClass

I think the best way to handle this is not to detect such code.


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.

@viralpraxis viralpraxis force-pushed the fix-style-float-division-cop-error-on-coercion-implicit-receiver branch from bab3e46 to cf26ed5 Compare January 2, 2025 10:38
PATTERN
# @!method any_coerce?(node)
def_node_matcher :any_coerce?, <<~PATTERN
{(send _ :/ (send _ :to_f)) (send (send _ :to_f) :/ _)}
{(send _ :/ (send !nil? :to_f)) (send (send !nil? :to_f) :/ _)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you organize the corrections to follow the don't-repeat-yourself as shown below?

diff --git a/lib/rubocop/cop/style/float_division.rb b/lib/rubocop/cop/style/float_division.rb
index 790f6e19a..8a4022a77 100644
--- a/lib/rubocop/cop/style/float_division.rb
+++ b/lib/rubocop/cop/style/float_division.rb
@@ -65,19 +65,32 @@ module RuboCop

         # @!method right_coerce?(node)
         def_node_matcher :right_coerce?, <<~PATTERN
-          (send _ :/ (send !nil? :to_f))
+          (send _ :/ #to_f_method?)
         PATTERN
+
         # @!method left_coerce?(node)
         def_node_matcher :left_coerce?, <<~PATTERN
-          (send (send !nil? :to_f) :/ _)
+          (send #to_f_method? :/ _)
         PATTERN
+
         # @!method both_coerce?(node)
         def_node_matcher :both_coerce?, <<~PATTERN
-          (send (send !nil? :to_f) :/ (send !nil? :to_f))
+          (send #to_f_method? :/ #to_f_method?)
         PATTERN
+
         # @!method any_coerce?(node)
         def_node_matcher :any_coerce?, <<~PATTERN
-          {(send _ :/ (send !nil? :to_f)) (send (send !nil? :to_f) :/ _)}
+          {(send _ :/ #to_f_method?) (send #to_f_method? :/ _)}
+        PATTERN
+
+        # @!method any_coerce?(node)
+        def_node_matcher :any_coerce?, <<~PATTERN
+          {(send _ :/ #to_f_method?) (send #to_f_method? :/ _)}
+        PATTERN
+
+        # @!method to_f_method?(node)
+        def_node_matcher :to_f_method?, <<~PATTERN
+          (send !nil? :to_f)
         PATTERN

         def on_send(node)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears to be a refactoring unrelated to the bug fix itself, but that's just my two cents 🙂

Done.

Cop `Style/FloatDivision` fails on implicit `#to_f` receiver.

With `single_coerce` style:

```console
echo 'a.to_f / to_f' | rubocop --only Style/FloatDivision -c <(echo 'Style/FloatDivision:\n EnforcedStyle: single_coerce') --stdin bug.rb -d

An error occurred while Style/FloatDivision cop was inspecting bug.rb:1:0.
Expected a Parser::Source::Range, Comment or RuboCop::AST::Node, got NilClass
lib/rubocop/cop/corrector.rb:111:in `to_range'
lib/rubocop/cop/corrector.rb:120:in `check_range_validity'
lib/parser/source/tree_rewriter.rb:398:in `combine'
lib/parser/source/tree_rewriter.rb:194:in `replace'
lib/parser/source/tree_rewriter.rb:218:in `remove'
lib/rubocop/cop/style/float_division.rb:126:in `remove_to_f_method'
lib/rubocop/cop/style/float_division.rb:90:in `block in on_send'
```

With `left_coerce` or `right_coerce` or `fdiv`:

```console
echo 'a / to_f' | rubocop --only Style/FloatDivision -c <(echo 'Style/FloatDivision:\n EnforcedStyle: left_coerce') --stdin bug.rb -d

An error occurred while Style/FloatDivision cop was inspecting bug.rb:1:0.
Expected a Parser::Source::Range, Comment or RuboCop::AST::Node, got NilClass
```

I think the best way to handle this is not to detect such code.

Co-Authored-By: Koichi ITO <koic.ito@gmail.com>
@viralpraxis viralpraxis force-pushed the fix-style-float-division-cop-error-on-coercion-implicit-receiver branch from cf26ed5 to 22d25bd Compare January 2, 2025 22:10
@koic koic merged commit 4cdfcb5 into rubocop:master Jan 3, 2025
23 checks passed
@viralpraxis viralpraxis deleted the fix-style-float-division-cop-error-on-coercion-implicit-receiver branch January 3, 2025 07:39
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.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy