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

Add new Lint/RedundantWithObject cop #4796

Merged
merged 2 commits into from
Oct 2, 2017

Conversation

koic
Copy link
Member

@koic koic commented Sep 26, 2017

Follow up for #4708 (review).

This PR adds a cop @rrosenblum kindly suggested to me.

Feature

This cop checks for unneeded with_object.

% cat /tmp/example.rb
# frozen_string_literal: true

ary.each_with_object([]) do |v|
  v
end

ary.each.with_object([]) do |v|
  v
end
% bundle exec rubocop /tmp/example.rb
Inspecting 1 file
W

Offenses:

/tmp/example.rb:3:5: W: Use each instead of each_with_object.
ary.each_with_object([]) do |v|
    ^^^^^^^^^^^^^^^^^^^^
/tmp/example.rb:7:10: W: Remove redundant with_object.
ary.each.with_object([]) do |v|
         ^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected

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).
  • Used the same coding conventions as the rest of the project.
  • 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.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

new_source = autocorrect_source('ary.each.with_object([]) { |v| v }')

expect(new_source).to eq 'ary.each { |v| v }'
end
Copy link
Collaborator

@pocke pocke Sep 27, 2017

Choose a reason for hiding this comment

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

Could you add tests for without parens and/or do-end keyword block.

For example:

a.each_with_object([]) do |v|
end
a.each_with_object [] do |v|
end
a.each_with_object([]) {|v| v}
a.each_with_object [] {|v| v}

And the auto-correction for the first case generates a broken code.

Copy link
Member Author

@koic koic Sep 27, 2017

Choose a reason for hiding this comment

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

Oh, I didn't realize that. I'm going to look in detail later. Thanks for your advise!

Copy link
Member Author

Choose a reason for hiding this comment

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

I added some test cases and fixed it with 95b0c46.

@koic koic force-pushed the add_new_lint_redundant_with_object branch 3 times, most recently from 157cd0f to 581bcf0 Compare September 27, 2017 10:20
Copy link
Contributor

@rrosenblum rrosenblum left a comment

Choose a reason for hiding this comment

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

Aside from the questions about consistency with the with_index cop, this looks good to me.

The commits will need to be squashed.

.freeze
MSG_WITH_OBJECT = 'Remove redundant `with_object`.'.freeze

def_node_matcher :redundant_with_object?, <<-PATTERN
Copy link
Contributor

Choose a reason for hiding this comment

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

I was expecting this pattern to be the same as the one of with_index with index swapped for object, but this pattern is slightly different. The pattern for with_object has an extra (_) after with_object and before args. Is it needed for this pattern? Should it be added to the with_index pattern?

def_node_matcher :redundant_with_index?, <<-PATTERN
  (block
    $(send
      _ {:each_with_index :with_index})
    (args
      (arg _))
    ...)
PATTERN

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. As you pointed out, the with_index pattern of Lint/RedundantWithObject cop can be improved.

def with_object_range(send)
range_between(
send.loc.selector.begin_pos,
send.source.length
Copy link
Contributor

Choose a reason for hiding this comment

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

The end range is different than the with_index cop. I saw that this was changed from send.loc.selector.end_pos + 1 when tests were added for the method call without parentheses. Does this need to be changed in the with_index cop?

Copy link
Member Author

Choose a reason for hiding this comment

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

Current Lint/RedundantWithObject cop does not check the argument with_index. This is an oversight of me, in fact it would be better to handle the case with the with_index argument. It can be improved as an implementation of Lint/RedundantWithObject cop.

@koic koic force-pushed the add_new_lint_redundant_with_object branch 2 times, most recently from e6479e0 to 908a422 Compare September 30, 2017 00:47
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 2, 2017

@koic Can you do the improvements suggested by @rrosenblum?

@koic
Copy link
Member Author

koic commented Oct 2, 2017

@bbatsov Thanks for the mention. I'm considering improving Lint/RedundantWithIndex cop. On the other hand, I don't consider changing this Lint/RedundantWithObject cop.

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 2, 2017

Got it.

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 2, 2017

Any idea why the build is currently failing on JRuby?

@koic
Copy link
Member Author

koic commented Oct 2, 2017

I was not aware of the failing on JRuby. Since failure in master branch has been fixed, rebasing in the latest master branch may fix the failing on JRuby.

If not, investigation is necessary. In that case, I'd like to investigate.

@koic
Copy link
Member Author

koic commented Oct 2, 2017

@bbatsov May I git reset the merge commit 0f2d5f1 and try rebasing on the latest master branch?

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 2, 2017

Of course.

@koic
Copy link
Member Author

koic commented Oct 2, 2017

Thanks. I'll try.

@koic koic force-pushed the add_new_lint_redundant_with_object branch from 0f2d5f1 to b168b91 Compare October 2, 2017 10:34
@koic
Copy link
Member Author

koic commented Oct 2, 2017

CI has failed, so I will investigate. Please wait for a little while.

@koic koic force-pushed the add_new_lint_redundant_with_object branch from b168b91 to 343e83b Compare October 2, 2017 13:53
```console
% bundle exec rubocop -a lib/rubocop/cop/lint/redundant_with_object.rb
Inspecting 1 file
C

Offenses:

lib/rubocop/cop/lint/redundant_with_object.rb:45:31: C: [Corrected] Use
of positional arguments on #add_offense is deprecated.
            add_offense(node, with_object_range(send))
                              ^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
```
@koic koic force-pushed the add_new_lint_redundant_with_object branch from 343e83b to 0d1f22b Compare October 2, 2017 14:23
@koic
Copy link
Member Author

koic commented Oct 2, 2017

CI passed. What I did was rebase with the latest master branch, and I autocorrected it. 0d1f22b

@bbatsov bbatsov merged commit 320e8c8 into rubocop:master Oct 2, 2017
@koic koic deleted the add_new_lint_redundant_with_object branch October 3, 2017 00:17
koic added a commit to koic/rubocop that referenced this pull request Nov 1, 2017
Follow up of rubocop#4796 (comment).

Actually, it is only `with_index` method that accepts an offset argument.
`with_with_index` method doesn't accept offset argument.
However, I think that the same AST will be low maintenance cost.
bbatsov pushed a commit that referenced this pull request Nov 1, 2017
Follow up of #4796 (comment).

Actually, it is only `with_index` method that accepts an offset argument.
`with_with_index` method doesn't accept offset argument.
However, I think that the same AST will be low maintenance cost.
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

4 participants