rubocop / rubocop-performance

An extension of RuboCop focused on code performance checks.
https://docs.rubocop.org/rubocop-performance
MIT License
684 stars 81 forks source link

Autocorrecting with `Performance/BlockGivenWithExplicitBlock` and `Lint/UnusedMethodArgument` breaks code #367

Closed ymap closed 1 year ago

ymap commented 1 year ago

When both Performance/BlockGivenWithExplicitBlock and Lint/UnusedMethodArgument cops are enabled, there seem to be cases where the autocorrect introduces incorrect behavior. This specifically occurs in method definitions with the following characteristics:

Here's a sample code to illustrate the issue:

def foo(&block)
  block_given?
end

Expected Behavior

Only one of the cop's autocorrects should be executed. Ideally, the code should be:

def foo
  block_given?
end

or

def foo(&block)
  block
end

Actual Behavior

The autocorrect results in:

def foo
  block
end

This leads to the code referencing an undefined variable block, thus deviating from the intended functionality.

Steps to Reproduce

  1. Save the code snippet below as example.rb.
def foo(&block)
  block_given?
end
  1. Run bundle exec rubocop -a example.rb.

RuboCop Version

$ bundle exec rubocop -V
1.56.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.0.2) [x86_64-linux]
  - rubocop-performance 1.19.0
  - rubocop-rspec 2.22.0