Skip to content

Fix false negative in Style/ArgumentsForwarding when a block is forwarded but other args aren't#12541

Merged
bbatsov merged 2 commits intorubocop:masterfrom
dvandersluis:fix/arguments-forwarding
Dec 19, 2023
Merged

Fix false negative in Style/ArgumentsForwarding when a block is forwarded but other args aren't#12541
bbatsov merged 2 commits intorubocop:masterfrom
dvandersluis:fix/arguments-forwarding

Conversation

@dvandersluis
Copy link
Member

The following code gets registered as a false negative by Style/ArgumentsForwarding

def foo(*args, &block)
  bar(*args, &block)
  baz(&block)
end

Which results in the following invalid autocorrection (&block is now longer defined):

def foo(...)
  bar(...)
  baz(&block)
end

This change fixes that case by considering block arguments when determining if forwarding is possible.

This had two side effects:

  1. The test for different argument names failed due to the block parameter now being considered when determining if there is an offense. To fix this, we now check that the rest and kwrest arguments have the same name.
  2. This revealed a few offenses in rubocop code where only a block argument should have been replaced with forwarding.

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@dvandersluis dvandersluis force-pushed the fix/arguments-forwarding branch 2 times, most recently from 0a41f4d to 03180e7 Compare December 14, 2023 20:03
@dvandersluis dvandersluis force-pushed the fix/arguments-forwarding branch from 03180e7 to 7a72839 Compare December 14, 2023 20:04
@dvandersluis dvandersluis requested a review from bbatsov December 18, 2023 20:19
@bbatsov bbatsov merged commit 1b3c31b into rubocop:master Dec 19, 2023
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 19, 2023

Looks good! Thanks for tackling this!

@mvz
Copy link
Contributor

mvz commented Jan 15, 2024

The suggested correction seems to change the method's arity, which could be the wrong thing to do.

@mvz
Copy link
Contributor

mvz commented Jan 15, 2024

See #12618.

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.

3 participants