diff --git a/changelog/fix_an_incorrect_autocorrect_for_performance_block_given_with_explicit_block.md b/changelog/fix_an_incorrect_autocorrect_for_performance_block_given_with_explicit_block.md new file mode 100644 index 0000000000..61a6921ccc --- /dev/null +++ b/changelog/fix_an_incorrect_autocorrect_for_performance_block_given_with_explicit_block.md @@ -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][]) diff --git a/lib/rubocop-performance.rb b/lib/rubocop-performance.rb index d7865b5453..37a7ceb49c 100644 --- a/lib/rubocop-performance.rb +++ b/lib/rubocop-performance.rb @@ -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 +) diff --git a/lib/rubocop/cop/performance/block_given_with_explicit_block.rb b/lib/rubocop/cop/performance/block_given_with_explicit_block.rb index 4c717d2774..a3d804694a 100644 --- a/lib/rubocop/cop/performance/block_given_with_explicit_block.rb +++ b/lib/rubocop/cop/performance/block_given_with_explicit_block.rb @@ -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 diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index 100368aa10..ebe8746cf7 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -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)