Skip to content

Commit

Permalink
[Fix #12117] Fix Style/ArgumentsForwarding when not always forwardi…
Browse files Browse the repository at this point in the history
…ng a block
  • Loading branch information
owst committed Aug 13, 2023
1 parent d929f68 commit 62a7cb9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12117](https://github.com/rubocop/rubocop/issues/12117): Fix a false positive for `Style/ArgumentsForwarding` cop when not always forwarding block. ([@owst][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/arguments_forwarding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def classification

if can_forward_all?
:all
elsif target_ruby_version >= 3.2
else
:rest_or_kwrest
end
end
Expand Down
90 changes: 90 additions & 0 deletions spec/rubocop/cop/style/arguments_forwarding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,34 @@ def foo(*args, &block)
RUBY
end

it 'does not register an offense when not always passing the block as well as restarg' do
expect_no_offenses(<<~RUBY)
def foo(*args, &block)
bar(*args, &block)
baz(*args)
end
RUBY
end

it 'does not register an offense when not always passing the block as well as kwrestarg' do
expect_no_offenses(<<~RUBY)
def foo(**kwargs, &block)
bar(**kwargs, &block)
baz(**kwargs)
end
RUBY
end

it 'does not register an offense when not always forwarding all' do
expect_no_offenses(<<~RUBY)
def foo(*args, **kwargs, &block)
bar(*args, **kwargs, &block)
bar(*args, &block)
bar(**kwargs, &block)
end
RUBY
end

it 'does not register an offense when body of method definition is empty' do
expect_no_offenses(<<~RUBY)
def foo(*args, &block)
Expand Down Expand Up @@ -956,6 +984,68 @@ def foo(*, **, &block)
RUBY
end

it 'registers an offense when not always passing the block as well as restarg' do
expect_offense(<<~RUBY)
def foo(*args, &block)
^^^^^ Use anonymous positional arguments forwarding (`*`).
bar(*args, &block)
^^^^^ Use anonymous positional arguments forwarding (`*`).
baz(*args)
^^^^^ Use anonymous positional arguments forwarding (`*`).
end
RUBY

expect_correction(<<~RUBY)
def foo(*, &block)
bar(*, &block)
baz(*)
end
RUBY
end

it 'registers an offense when not always passing the block as well as kwrestarg' do
expect_offense(<<~RUBY)
def foo(**kwargs, &block)
^^^^^^^^ Use anonymous keyword arguments forwarding (`**`).
bar(**kwargs, &block)
^^^^^^^^ Use anonymous keyword arguments forwarding (`**`).
baz(**kwargs)
^^^^^^^^ Use anonymous keyword arguments forwarding (`**`).
end
RUBY

expect_correction(<<~RUBY)
def foo(**, &block)
bar(**, &block)
baz(**)
end
RUBY
end

it 'registers an offense when not always forwarding all' do
expect_offense(<<~RUBY)
def foo(*args, **kwargs, &block)
^^^^^ Use anonymous positional arguments forwarding (`*`).
^^^^^^^^ Use anonymous keyword arguments forwarding (`**`).
bar(*args, **kwargs, &block)
^^^^^ Use anonymous positional arguments forwarding (`*`).
^^^^^^^^ Use anonymous keyword arguments forwarding (`**`).
bar(*args, &block)
^^^^^ Use anonymous positional arguments forwarding (`*`).
bar(**kwargs, &block)
^^^^^^^^ Use anonymous keyword arguments forwarding (`**`).
end
RUBY

expect_correction(<<~RUBY)
def foo(*, **, &block)
bar(*, **, &block)
bar(*, &block)
bar(**, &block)
end
RUBY
end

it 'registers an offense for restarg when passing block to separate call' do
expect_offense(<<~RUBY)
def foo(*args, &block)
Expand Down

0 comments on commit 62a7cb9

Please sign in to comment.