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

Default configuration for UnusedMethodArgument+ExplicitBlockArgument breaking existing code #9955

Closed
Drowze opened this issue Jul 28, 2021 · 0 comments · Fixed by #9967
Closed
Labels

Comments

@Drowze
Copy link
Contributor

Drowze commented Jul 28, 2021

Expected behavior

I expect Rubocop not to break existing code.

Actual behavior

When running autocorrection on rubocop default configuration, it breaks my Ruby script

Steps to reproduce the problem

Given a default rubocop configuration

Given test.rb:

def my_method(&block)
  [1, 2, 3].map { yield }
end

puts my_method { 'hello' }

When I run rubocop --debug -a test.rb

It autocorrects the file to: (notice block will raise a undefined local variable error)

def my_method
  [1, 2, 3].map(&block)
end

puts my_method { 'hello' }

And I get this output:

For /Users/Drowze/workspace/ruby-scripts: Default configuration from /Users/Drowze/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/rubocop-1.18.4/config/default.yml
Inspecting 1 file
Scanning /Users/Drowze/workspace/ruby-scripts/test.rb
Loading cache from /Users/Drowze/.cache/rubocop_cache/f0c6549b3ba85dee6fbdac52c969fbbd0c5e7eeb/0536d18c9eb040c724be8df71fd74a67730b58ef/551f83ba8d8268e0ebf3d6681dc8d5840df5f64c
W

Offenses:

test.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
def my_method
^
test.rb:1:14: C: [Corrected] Style/DefWithParentheses: Omit the parentheses in defs when the method doesn't accept any arguments.
def my_method()
             ^
test.rb:1:16: W: [Corrected] Lint/UnusedMethodArgument: Unused method argument - block. If it's necessary, use _ or _block as an argument name to indicate that it won't be used. You can also write as my_method(*) if you want the method to accept any arguments but don't care about them.
def my_method(&block)
               ^^^^^
test.rb:2:3: C: [Corrected] Style/ExplicitBlockArgument: Consider using explicit block argument in the surrounding method's signature over yield.
  [1, 2, 3].map { yield }
  ^^^^^^^^^^^^^^^^^^^^^^^
test.rb:5:1: W: Lint/AmbiguousBlockAssociation: Parenthesize the param my_method { 'hello' } to make sure that the block will be associated with the my_method method call.
puts my_method { 'hello' }
^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 5 offenses detected, 3 offenses corrected, 1 more offense can be corrected with `rubocop -A`
Finished in 0.25655099999858066 seconds

RuboCop version

$ rubocop -V
1.18.4 (using Parser 3.0.2.0, rubocop-ast 1.8.0, running on ruby 2.7.3 x86_64-darwin20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants