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 Rails/Presence cop when #present? or #blank? is used in if or unless modifier #5238

Merged
merged 3 commits into from
Dec 20, 2017

Conversation

eitoball
Copy link
Contributor

@eitoball eitoball commented Dec 13, 2017

This PR should fix Rails/Presence cop when #present? on #blank? is used in if or unless modifier.

Below is a sample output that represents bug.

$ cat sample.rb
# frozen_string_literal: true

v if v.present?
$ rubocop -c .rubocop_empty.yml -d --rails sample.rb
bundle exec rubocop -c .rubocop_empty.yml -d --rails sample.rb
configuration from /.../.rubocop_empty.yml
Default configuration from /.../gems/rubocop-0.52.0/config/default.yml                                             Inheriting configuration from /.../gems/rubocop-0.52.0/config/enabled.yml
Inheriting configuration from /.../gems/rubocop-0.52.0/config/disabled.yml
Inspecting 1 file
Scanning /.../sample.rb
An error occurred while Rails/Presence cop was inspecting /.../sample.rb:3:0.
undefined method `nil_type?' for nil:NilClass
/.../gems/rubocop-0.52.0/lib/rubocop/cop/rails/presence.rb:99:in `replacement'
/.../gems/rubocop-0.52.0/lib/rubocop/cop/rails/presence.rb:80:in `on_if'
<snip>
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Rails/Presence cop was inspecting /.../sample.rb:3:0.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
Mention the following information in the issue report:
0.52.0 (using Parser 2.4.0.2, running on ruby 2.4.2 x86_64-darwin16)
Finished in 0.22184800007380545 seconds

Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run rake default or rake parallel. It executes all tests and RuboCop for itself, and generates the documentation.


it 'does not register an offense when if or unless modifier is used ' do
[
'foo if foo.present?',
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case, I think it should register an offense for this code...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to register this pattern as offense, but I could not get it right. Could somebody help me?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably we should rewrite the redundant_receiver_and_other and/or redundant_negative_receiver_and_other patterns. Let's try to rewrite the patterns with a documentation of Node Pattern, or replace the expect_no_offenses with expect_offense and make pending the test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your advice. I didn't have to rewrite neither redundant_receiver_and_other or redundant_negative_receiver_and_other. What I did was that adding a unless a.blank? to offense.

@wata727
Copy link
Contributor

wata727 commented Dec 13, 2017

Thank you for fixing. I think this is a serious bug 🙇‍♂️

@@ -75,7 +75,7 @@ def on_if(node)
unless receiver
receiver, other = redundant_negative_receiver_and_other(node)
end
return unless receiver
return unless receiver && other
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe it should use blocks.
For example:

redundant_receiver_and_other(node)  do |receiver, other|
  # ...
end

redundant_negative_receiver_and_other(node) do |receiver, other|
  # ...
end

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good idea!

@bbatsov bbatsov merged commit e18eb06 into rubocop:master Dec 20, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 20, 2017

👍

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.

4 participants