KEMBAR78
[Fix #14535] Fix autocorrect for `Style/ExplicitBlockArgument` when there are two methods that share the same implementation by Earlopain · Pull Request #14537 · rubocop/rubocop · GitHub
Skip to content

Conversation

@Earlopain
Copy link
Contributor

@Earlopain Earlopain commented Sep 18, 2025

Fix #14535

Nodes are only compared by their content, not location. So it fails to place the def argument in the second case.

Also added a test that a nested method definition places the block argument at the correct def node.


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.

@Earlopain Earlopain force-pushed the explicit-block-duplicate-implementation branch from 800660f to 3155426 Compare September 18, 2025 15:26
Comment on lines 295 to 310
expect_offense(<<~RUBY)
def foo
def bar.baz
bat { |xyz| yield xyz }
^^^^^^^^^^^^^^^^^^^^^^^ Consider using explicit block argument in the surrounding method's signature over `yield`.
end
end
RUBY

expect_correction(<<~RUBY)
def foo
def bar.baz(&block)
bat(&block)
end
end
RUBY
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That just my two cents. The variable name bat might feel a bit abrupt. It could be better to replace it with a metasyntactic variable like the following, for example.

Suggested change
expect_offense(<<~RUBY)
def foo
def bar.baz
bat { |xyz| yield xyz }
^^^^^^^^^^^^^^^^^^^^^^^ Consider using explicit block argument in the surrounding method's signature over `yield`.
end
end
RUBY
expect_correction(<<~RUBY)
def foo
def bar.baz(&block)
bat(&block)
end
end
RUBY
expect_offense(<<~RUBY)
def foo
def bar.baz
qux { |quux| yield quux }
^^^^^^^^^^^^^^^^^^^^^^^ Consider using explicit block argument in the surrounding method's signature over `yield`.
end
end
RUBY
expect_correction(<<~RUBY)
def foo
def bar.baz(&block)
qux(&block)
end
end
RUBY

… when there are two methods that share the same implementation

Nodes are only compared by their content, not location.
So it fails to place the def argument in the second case.

Also added a test that a nested method definition
places the block argument at the correct def node.
@Earlopain Earlopain force-pushed the explicit-block-duplicate-implementation branch from 3155426 to d42e351 Compare September 18, 2025 15:41
@dvandersluis dvandersluis merged commit 858d9a8 into rubocop:master Sep 18, 2025
22 checks passed
@dvandersluis
Copy link
Member

Thanks @Earlopain!

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.

Style/ExplicitBlockArgument sometimes forget to give block arguments when --autocorrect

3 participants