Skip to content

Commit

Permalink
[Fix #367] Fix an incorrect autocorrect for `Performance/BlockGivenWi…
Browse files Browse the repository at this point in the history
…thExplicitBlock`

Fixes #367.

This PR fixes an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock`
when using `Lint/UnusedMethodArgument`'s autocorrection together.
  • Loading branch information
ymap committed Sep 12, 2023
1 parent 4a58fbc commit 0dc9a18
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
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

0 comments on commit 0dc9a18

Please sign in to comment.