Skip to content

Commit

Permalink
[Fix #12601] Make Style/EachForSimpleLoop accept block with no para…
Browse files Browse the repository at this point in the history
…meters

Fixes #12601.

This PR makes `Style/EachForSimpleLoop` accept block with no parameters.

This is the behavior according to the documentation:

> This check only applies if the block takes no parameters.

https://docs.rubocop.org/rubocop/1.59/cops_style.html#styleeachforsimpleloop
  • Loading branch information
koic authored and bbatsov committed Jan 12, 2024
1 parent 075d4d5 commit b36fe4a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
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

0 comments on commit b36fe4a

Please sign in to comment.