Skip to content

Commit

Permalink
Merge pull request #368 from ymap/fix_an_incorrect_autocorrect_for_pe…
Browse files Browse the repository at this point in the history
…rformance_block_given_with_explicit_block

[Fix #367] Fix an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock`
  • Loading branch information
SerhiiMisiura committed Sep 12, 2023
2 parents 9f9789d + a7feb9b commit 18dc0ee
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 18dc0ee

Please sign in to comment.