From 0dc9a18a7ccbc0522efe89c35290852ece8942bd Mon Sep 17 00:00:00 2001 From: ymap Date: Tue, 12 Sep 2023 05:40:22 +0000 Subject: [PATCH] [Fix #367] Fix an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock` Fixes #367. This PR fixes an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock` when using `Lint/UnusedMethodArgument`'s autocorrection together. --- ...rformance_block_given_with_explicit_block.md | 1 + lib/rubocop-performance.rb | 8 ++++++++ .../block_given_with_explicit_block.rb | 4 ++++ spec/rubocop/cli/autocorrect_spec.rb | 17 +++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 changelog/fix_an_incorrect_autocorrect_for_performance_block_given_with_explicit_block.md 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)