-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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 Style/RedundantEach
cop
#11110
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#11110](https://github.com/rubocop/rubocop/pull/11110): Add new `Style/RedundantEach` cop. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# Checks for redundant `each`. | ||
# | ||
# @safety | ||
# This cop is unsafe, as it can produce false positives if the receiver | ||
# is not an `Enumerable`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# array.each.each { |v| do_something(v) } | ||
# | ||
# # good | ||
# array.each { |v| do_something(v) } | ||
# | ||
# # bad | ||
# array.each.each_with_index { |v, i| do_something(v, i) } | ||
# | ||
# # good | ||
# array.each.with_index { |v, i| do_something(v, i) } | ||
# array.each_with_index { |v, i| do_something(v, i) } | ||
# | ||
# # bad | ||
# array.each.each_with_object { |v, o| do_something(v, o) } | ||
# | ||
# # good | ||
# array.each.with_object { |v, o| do_something(v, o) } | ||
# array.each_with_object { |v, o| do_something(v, o) } | ||
# | ||
class RedundantEach < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Remove redundant `each`.' | ||
MSG_WITH_INDEX = 'Use `with_index` to remove redundant `each`.' | ||
MSG_WITH_OBJECT = 'Use `with_object` to remove redundant `each`.' | ||
|
||
RESTRICT_ON_SEND = %i[each each_with_index each_with_object].freeze | ||
|
||
def on_send(node) | ||
return unless (redundant_node = redundant_each_method(node)) | ||
|
||
range = range(node) | ||
|
||
add_offense(range, message: message(node)) do |corrector| | ||
case node.method_name | ||
when :each | ||
remove_redundant_each(corrector, range, redundant_node) | ||
when :each_with_index | ||
corrector.replace(node.loc.selector, 'with_index') | ||
when :each_with_object | ||
corrector.replace(node.loc.selector, 'with_object') | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity | ||
def redundant_each_method(node) | ||
if node.method?(:each) && !node.parent.block_type? | ||
ancestor_node = node.each_ancestor(:send).detect do |ancestor| | ||
RESTRICT_ON_SEND.include?(ancestor.method_name) || ancestor.method?(:reverse_each) | ||
end | ||
end | ||
|
||
ancestor_node || node.each_descendant(:send).detect do |descendant| | ||
next if descendant.parent.block_type? | ||
|
||
detected = descendant.method_name.to_s.start_with?('each_') unless node.method?(:each) | ||
|
||
detected || descendant.method?(:reverse_each) | ||
end | ||
end | ||
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity | ||
|
||
def range(node) | ||
if node.method?(:each) | ||
node.loc.dot.join(node.loc.selector) | ||
else | ||
node.loc.selector | ||
end | ||
end | ||
|
||
def message(node) | ||
case node.method_name | ||
when :each | ||
MSG | ||
when :each_with_index | ||
MSG_WITH_INDEX | ||
when :each_with_object | ||
MSG_WITH_OBJECT | ||
end | ||
end | ||
|
||
def remove_redundant_each(corrector, range, redundant_node) | ||
corrector.remove(range) | ||
|
||
if redundant_node.method?(:each_with_index) | ||
corrector.replace(redundant_node.loc.selector, 'each.with_index') | ||
elsif redundant_node.method?(:each_with_object) | ||
corrector.replace(redundant_node.loc.selector, 'each.with_object') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::RedundantEach, :config do | ||
it 'registers an offense when using `each.each`' do | ||
expect_offense(<<~RUBY) | ||
array.each.each { |v| do_something(v) } | ||
^^^^^ Remove redundant `each`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.each { |v| do_something(v) } | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `each.each_with_index`' do | ||
expect_offense(<<~RUBY) | ||
array.each.each_with_index { |v| do_something(v) } | ||
^^^^^ Remove redundant `each`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.each.with_index { |v| do_something(v) } | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `each.each_with_object`' do | ||
expect_offense(<<~RUBY) | ||
array.each.each_with_object([]) { |v, o| do_something(v, o) } | ||
^^^^^ Remove redundant `each`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.each.with_object([]) { |v, o| do_something(v, o) } | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using a method starting with `each_` with `each_with_index`' do | ||
expect_offense(<<~RUBY) | ||
context.each_child_node.each_with_index.any? do |node, index| | ||
^^^^^^^^^^^^^^^ Use `with_index` to remove redundant `each`. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
context.each_child_node.with_index.any? do |node, index| | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using a method starting with `each_` with `each_with_object`' do | ||
expect_offense(<<~RUBY) | ||
context.each_child_node.each_with_object([]) do |node, object| | ||
^^^^^^^^^^^^^^^^ Use `with_object` to remove redundant `each`. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
context.each_child_node.with_object([]) do |node, object| | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `reverse_each.each`' do | ||
expect_offense(<<~RUBY) | ||
context.reverse_each.each { |i| do_something(i) } | ||
^^^^^ Remove redundant `each`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
context.reverse_each { |i| do_something(i) } | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `each.reverse_each`' do | ||
expect_offense(<<~RUBY) | ||
context.each.reverse_each { |i| do_something(i) } | ||
^^^^^ Remove redundant `each`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
context.reverse_each { |i| do_something(i) } | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `reverse_each.each_with_index`' do | ||
expect_offense(<<~RUBY) | ||
context.reverse_each.each_with_index.any? do |node, index| | ||
^^^^^^^^^^^^^^^ Use `with_index` to remove redundant `each`. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
context.reverse_each.with_index.any? do |node, index| | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `reverse_each.each_with_object`' do | ||
expect_offense(<<~RUBY) | ||
scope_stack.reverse_each.each_with_object([]) do |scope, variables| | ||
^^^^^^^^^^^^^^^^ Use `with_object` to remove redundant `each`. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
scope_stack.reverse_each.with_object([]) do |scope, variables| | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using only single `each`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.each { |v| do_something(v) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `each.with_index`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.each.with_index { |v, i| do_something(v, i) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `each_with_index`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.each_with_index { |v, i| do_something(v, i) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `each.with_object`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.each.with_object { |v, o| do_something(v, o) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `each_with_object`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.each_with_object { |v, o| do_something(v, o) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `each_with_index.reverse_each`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.each_with_index.reverse_each do |value, index| | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `each_ancestor.each`' do | ||
expect_no_offenses(<<~RUBY) | ||
node.each_ancestor(:def, :defs, :block).each do |ancestor| | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `reverse_each {}.each {}`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.reverse_each { |i| foo(i) }.each { |i| bar(i) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `each {}.reverse_each {}`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.each { |i| foo(i) }.reverse_each { |i| bar(i) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `each {}.each_with_index {}`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.each { |i| foo(i) }.each_with_index { |i| bar(i) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `each {}.each_with_object([]) {}`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.each { |i| foo(i) }.each_with_object([]) { |i| bar(i) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `each_foo {}.each_with_object([]) {}`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.each_foo { |i| foo(i) }.each_with_object([]) { |i| bar(i) } | ||
RUBY | ||
end | ||
end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing that using
each_with_index
directly is also good. Same witheach_with_object
. Probably we'll need some separate cop about such iterators (e.g. something suggestingeach.with_object
overeach_ with_object
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure,
each_with_index
is also accepted. I've updated it!OTOH, I did a quick check and saw no difference in performance. This autocorrection chooses
each.with_object
because the receiver is anEnumerator
and using method chain. But, I'm not sure which ofeach_with_object
andeach.with_object
is definitely better as the default when receiver is a collection object (e.g. instance of Array, Hash, Set).