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

[Fix #367] Fix an incorrect autocorrect for Performance/BlockGivenWithExplicitBlock #368

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#367](https://github.com/rubocop/rubocop-performance/issues/367): Fix an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock` when using `Lint/UnusedMethodArgument`'s autocorrection together. ([@ymap][])
8 changes: 8 additions & 0 deletions lib/rubocop-performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@
RuboCop::Performance::Inject.defaults!

require_relative 'rubocop/cop/performance_cops'

RuboCop::Cop::Lint::UnusedMethodArgument.singleton_class.prepend(
Module.new do
def autocorrect_incompatible_with
super.push(RuboCop::Cop::Performance::BlockGivenWithExplicitBlock)
end
end
)
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def on_send(node)
corrector.replace(node, block_arg_name)
end
end

def self.autocorrect_incompatible_with
[Lint::UnusedMethodArgument]
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@
RUBY
end

it 'corrects `Performance/BlockGivenWithExplicitBlock` with `Lint/UnusedMethodArgument`' do
source = <<~RUBY
def foo(&block)
block_given?
end
RUBY
create_file('example.rb', source)
expect(
cli.run(['--autocorrect', '--only', 'Performance/BlockGivenWithExplicitBlock,Lint/UnusedMethodArgument'])
).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
def foo()
block_given?
end
RUBY
end

private

def create_file(file_path, content)
Expand Down