Skip to content

Commit

Permalink
[Fix rubocop#444] Fix an incorrect autocorrect for `Performance/Block…
Browse files Browse the repository at this point in the history
…GivenWithExplicitBlock`

Fixes rubocop#444.

This PR fixes an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock`
when using `Naming/BlockForwarding`'s autocorrection together.
  • Loading branch information
a-lavis committed Feb 16, 2024
1 parent dd698b2 commit 45e9530
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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][])
16 changes: 11 additions & 5 deletions lib/rubocop-performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def on_send(node)
end

def self.autocorrect_incompatible_with
[Lint::UnusedMethodArgument]
[Lint::UnusedMethodArgument, Naming::BlockForwarding]
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 45e9530

Please sign in to comment.