Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ymap opened this issue Aug 26, 2023 · 0 comments · Fixed by #368
Labels
bug Something isn't working

Comments

@ymap
Copy link
Contributor

ymap commented Aug 26, 2023

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:

  • The block argument is explicitly declared.
  • This argument remains unused within the method.
  • The method invokes block_given?.

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
ymap added a commit to ymap/rubocop-performance that referenced this issue Aug 26, 2023
…GivenWithExplicitBlock`

Fixes rubocop#367.

This PR fixes an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock`
when using `Lint/UnusedMethodArgument`'s autocorrection together.
ymap added a commit to ymap/rubocop-performance that referenced this issue Aug 26, 2023
…GivenWithExplicitBlock`

Fixes rubocop#367.

This PR fixes an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock`
when using `Lint/UnusedMethodArgument`'s autocorrection together.
ymap added a commit to ymap/rubocop-performance that referenced this issue Sep 12, 2023
…GivenWithExplicitBlock`

Fixes rubocop#367.

This PR fixes an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock`
when using `Lint/UnusedMethodArgument`'s autocorrection together.
@koic koic added the bug Something isn't working label Sep 12, 2023
@koic koic closed this as completed in #368 Sep 12, 2023
koic added a commit that referenced this issue Sep 12, 2023
…rformance_block_given_with_explicit_block

[Fix #367] Fix an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants