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 #12601] Make Style/EachForSimpleLoop accept block with no parameters #12609

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12601](https://github.com/rubocop/rubocop/issues/12601): Make `Style/EachForSimpleLoop` accept block with no parameters. ([@koic][])
8 changes: 4 additions & 4 deletions lib/rubocop/cop/style/each_for_simple_loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler

send_node = node.send_node

range = send_node.receiver.source_range.join(send_node.loc.selector)

add_offense(range) do |corrector|
add_offense(send_node) do |corrector|
range_type, min, max = each_range(node)

max += 1 if range_type == :irange

corrector.replace(node.send_node, "#{max - min}.times")
corrector.replace(send_node, "#{max - min}.times")
end
end

private

def offending?(node)
return false unless node.arguments.empty?

each_range_with_zero_origin?(node) || each_range_without_block_argument?(node)
end

Expand Down
64 changes: 52 additions & 12 deletions spec/rubocop/cop/style/each_for_simple_loop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,98 @@
expect_no_offenses('(0..b).each {}')
end

context 'with inline block with parameters' do
context 'with inline block with no parameters' do
it 'autocorrects an offense' do
expect_offense(<<~RUBY)
(0...10).each { |n| }
(0...10).each { do_something }
^^^^^^^^^^^^^ Use `Integer#times` for a simple loop which iterates a fixed number of times.
RUBY

expect_correction(<<~RUBY)
10.times { |n| }
10.times { do_something }
RUBY
end
end

context 'with multiline block with parameters' do
context 'with inline block with parameters' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
(0...10).each { |n| do_something(n) }
RUBY
end
end

context 'with multiline block with no parameters' do
it 'autocorrects an offense' do
expect_offense(<<~RUBY)
(0...10).each do |n|
(0...10).each do
^^^^^^^^^^^^^ Use `Integer#times` for a simple loop which iterates a fixed number of times.
do_something
end
RUBY

expect_correction(<<~RUBY)
10.times do |n|
10.times do
do_something
end
RUBY
end
end

context 'with multiline block with parameters' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
(0...10).each do |n|
do_something(n)
end
RUBY
end
end

context 'when using safe navigation operator' do
context 'with inline block with parameters' do
context 'with inline block with no parameters' do
it 'autocorrects an offense' do
expect_offense(<<~RUBY)
(0...10)&.each { |n| }
(0...10)&.each { do_something }
^^^^^^^^^^^^^^ Use `Integer#times` for a simple loop which iterates a fixed number of times.
RUBY

expect_correction(<<~RUBY)
10.times { |n| }
10.times { do_something }
RUBY
end
end

context 'with multiline block with parameters' do
context 'with inline block with parameters' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
(0...10)&.each { |n| do_something(n) }
RUBY
end
end

context 'with multiline block with no parameters' do
it 'autocorrects an offense' do
expect_offense(<<~RUBY)
(0...10)&.each do |n|
(0...10)&.each do
^^^^^^^^^^^^^^ Use `Integer#times` for a simple loop which iterates a fixed number of times.
do_something
end
RUBY

expect_correction(<<~RUBY)
10.times do |n|
10.times do
do_something
end
RUBY
end
end

context 'with multiline block with parameters' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
(0...10)&.each do |n|
do_something(n)
end
RUBY
end
Expand Down