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 #9599] Fix an error for Style/CaseLikeIf #9600

Merged
merged 1 commit into from Mar 16, 2021

Conversation

koic
Copy link
Member

@koic koic commented Mar 14, 2021

Fixes #9599.

This PR fixes an error for Style/CaseLikeIf when using include? without a receiver.


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 koic force-pushed the fix_error_for_style_case_like_if branch from aa22e6e to 36b15ec Compare March 14, 2021 16:01
Copy link
Collaborator

@bbatsov bbatsov left a comment

Choose a reason for hiding this comment

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

Looks good!

@pderichs
Copy link

pderichs commented Mar 14, 2021

I was about to post a similiar issue to #9599 after I found a similar situation in an old project.

Thanks for providing a fix @koic 👍

My spec which also seems to catch the situation was looking like this:

diff --git a/spec/rubocop/cop/style/case_like_if_spec.rb b/spec/rubocop/cop/style/case_like_if_spec.rb
index 0d52df5dc..68429a336 100644
--- a/spec/rubocop/cop/style/case_like_if_spec.rb
+++ b/spec/rubocop/cop/style/case_like_if_spec.rb
@@ -66,6 +66,15 @@ RSpec.describe RuboCop::Cop::Style::CaseLikeIf, :config do
     RUBY
   end

+  it 'does not register an offense when using cover? without a receiver' do
+    expect_no_offenses(<<~RUBY)
+      if x == 1
+      elsif cover?(Bar)
+      else
+      end
+    RUBY
+  end
+
   it 'does not register an offense when using `==` with class reference' do
     expect_no_offenses(<<~RUBY)
       if x == Foo

This spec seems to be still failing with these changes with a similar error:

     Failure/Error: node = node.children.last while node.begin_type?
     
     NoMethodError:
       undefined method `begin_type?' for nil:NilClass
     # ./lib/rubocop/cop/style/case_like_if.rb:227:in `deparenthesize'
     # ./lib/rubocop/cop/style/case_like_if.rb:175:in `condition_from_send_node'
     # ./lib/rubocop/cop/style/case_like_if.rb:156:in `collect_conditions'
     # ./lib/rubocop/cop/style/case_like_if.rb:48:in `block in on_if'
     # ./lib/rubocop/cop/style/case_like_if.rb:44:in `each'
     # ./lib/rubocop/cop/style/case_like_if.rb:44:in `on_if'
     # ./lib/rubocop/cop/commissioner.rb:100:in `public_send'
     # ./lib/rubocop/cop/commissioner.rb:100:in `block (2 levels) in trigger_responding_cops'
     # ./lib/rubocop/cop/commissioner.rb:166:in `with_cop_error_handling'
     # ./lib/rubocop/cop/commissioner.rb:99:in `block in trigger_responding_cops'
     # ./lib/rubocop/cop/commissioner.rb:98:in `each'
     # ./lib/rubocop/cop/commissioner.rb:98:in `trigger_responding_cops'
     # ./lib/rubocop/cop/commissioner.rb:69:in `on_if'
     # ./lib/rubocop/cop/commissioner.rb:86:in `investigate'
     # ./lib/rubocop/cop/team.rb:157:in `investigate_partial'
     # ./lib/rubocop/cop/team.rb:83:in `investigate'
     # ./lib/rubocop/rspec/cop_helper.rb:50:in `_investigate'
     # ./lib/rubocop/rspec/cop_helper.rb:21:in `inspect_source'
     # ./lib/rubocop/rspec/expect_offense.rb:178:in `expect_no_offenses'
     # ./spec/rubocop/cop/style/case_like_if_spec.rb:169:in `block (2 levels) in <top (required)>'

@koic koic force-pushed the fix_error_for_style_case_like_if branch from 36b15ec to add9f51 Compare March 14, 2021 18:03
@koic
Copy link
Member Author

koic commented Mar 14, 2021

@pderichs Thank you for quick feedback! I confirmed this and updated this PR!

@pderichs
Copy link

Works perfectly. Thank you @koic 🙇‍♂️

Fixes rubocop#9599.

This PR fixes an error for `Style/CaseLikeIf`
when using `include?` without a receiver.
@koic koic force-pushed the fix_error_for_style_case_like_if branch from add9f51 to 62d0620 Compare March 16, 2021 01:40
@bbatsov bbatsov merged commit f18b7b6 into rubocop:master Mar 16, 2021
@koic koic deleted the fix_error_for_style_case_like_if branch March 16, 2021 07:29
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.

Style/CaseLikeIf: undefined method `begin_type?' for nil:NilClass
3 participants