From 45e953016d9ce52fec19baff1a5981f0e8d41192 Mon Sep 17 00:00:00 2001 From: Aidan Date: Fri, 16 Feb 2024 15:04:43 -0500 Subject: [PATCH] [Fix #444] Fix an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock` Fixes #444. This PR fixes an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock` when using `Naming/BlockForwarding`'s autocorrection together. --- ...ormance_block_given_with_explicit_block.md | 1 + lib/rubocop-performance.rb | 16 +++++++++++----- .../block_given_with_explicit_block.rb | 2 +- spec/rubocop/cli/autocorrect_spec.rb | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) 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..c75aae1e13 --- /dev/null +++ b/changelog/fix_an_incorrect_autocorrect_for_performance_block_given_with_explicit_block.md @@ -0,0 +1 @@ +* [#444](https://github.com/rubocop/rubocop-performance/issues/444): Fix an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock` when using `Naming/BlockForwarding`'s autocorrection together. ([@a-lavis][]) diff --git a/lib/rubocop-performance.rb b/lib/rubocop-performance.rb index 37a7ceb49c..26917a90c4 100644 --- a/lib/rubocop-performance.rb +++ b/lib/rubocop-performance.rb @@ -10,10 +10,16 @@ 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 +autocorrect_incompatible_with_block_given_with_explicit_block = Module.new do + def autocorrect_incompatible_with + super.push(RuboCop::Cop::Performance::BlockGivenWithExplicitBlock) end +end + +RuboCop::Cop::Lint::UnusedMethodArgument.singleton_class.prepend( + autocorrect_incompatible_with_block_given_with_explicit_block +) + +RuboCop::Cop::Naming::BlockForwarding.singleton_class.prepend( + autocorrect_incompatible_with_block_given_with_explicit_block ) 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 a3d804694a..22300aea7d 100644 --- a/lib/rubocop/cop/performance/block_given_with_explicit_block.rb +++ b/lib/rubocop/cop/performance/block_given_with_explicit_block.rb @@ -49,7 +49,7 @@ def on_send(node) end def self.autocorrect_incompatible_with - [Lint::UnusedMethodArgument] + [Lint::UnusedMethodArgument, Naming::BlockForwarding] end end end diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index ebe8746cf7..d96fd2f0e8 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -47,6 +47,25 @@ def foo() RUBY end + it 'corrects `Performance/BlockGivenWithExplicitBlock` with `Naming/BlockForwarding`' do + source = <<~RUBY + def foo(&block) + block_given? + bar(&block) + end + RUBY + create_file('example.rb', source) + expect( + cli.run(['--autocorrect', '--only', 'Performance/BlockGivenWithExplicitBlock,Naming/BlockForwarding']) + ).to eq(0) + expect(File.read('example.rb')).to eq(<<~RUBY) + def foo(&block) + block + bar(&block) + end + RUBY + end + private def create_file(file_path, content)