Skip to content

Commit 349cd52

Browse files
authored
Merge pull request #445 from a-lavis/block_given_with_explicit_block_incompatible_with_block_forwarding
[Fix #444] Fix an incorrect autocorrect for `Performance/BlockGivenWi…
2 parents 7758046 + 45e9530 commit 349cd52

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop-performance.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@
77
require_relative 'rubocop/performance/plugin'
88
require_relative 'rubocop/cop/performance_cops'
99

10-
RuboCop::Cop::Lint::UnusedMethodArgument.singleton_class.prepend(
11-
Module.new do
12-
def autocorrect_incompatible_with
13-
super.push(RuboCop::Cop::Performance::BlockGivenWithExplicitBlock)
14-
end
10+
autocorrect_incompatible_with_block_given_with_explicit_block = Module.new do
11+
def autocorrect_incompatible_with
12+
super.push(RuboCop::Cop::Performance::BlockGivenWithExplicitBlock)
1513
end
14+
end
15+
16+
RuboCop::Cop::Lint::UnusedMethodArgument.singleton_class.prepend(
17+
autocorrect_incompatible_with_block_given_with_explicit_block
18+
)
19+
20+
RuboCop::Cop::Naming::BlockForwarding.singleton_class.prepend(
21+
autocorrect_incompatible_with_block_given_with_explicit_block
1622
)

lib/rubocop/cop/performance/block_given_with_explicit_block.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def on_send(node)
5252
end
5353

5454
def self.autocorrect_incompatible_with
55-
[Lint::UnusedMethodArgument]
55+
[Lint::UnusedMethodArgument, Naming::BlockForwarding]
5656
end
5757
end
5858
end

spec/rubocop/cli/autocorrect_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ def foo()
5353
RUBY
5454
end
5555

56+
it 'corrects `Performance/BlockGivenWithExplicitBlock` with `Naming/BlockForwarding`' do
57+
source = <<~RUBY
58+
def foo(&block)
59+
block_given?
60+
bar(&block)
61+
end
62+
RUBY
63+
create_file('example.rb', source)
64+
expect(
65+
cli.run(['--autocorrect', '--only', 'Performance/BlockGivenWithExplicitBlock,Naming/BlockForwarding'])
66+
).to eq(0)
67+
expect(File.read('example.rb')).to eq(<<~RUBY)
68+
def foo(&block)
69+
block
70+
bar(&block)
71+
end
72+
RUBY
73+
end
74+
5675
private
5776

5877
def create_file(file_path, content)

0 commit comments

Comments
 (0)