Skip to content

Commit ded9641

Browse files
authored
Merge pull request #368 from ymap/fix_an_incorrect_autocorrect_for_performance_block_given_with_explicit_block
[Fix #367] Fix an incorrect autocorrect for `Performance/BlockGivenWithExplicitBlock`
2 parents 4a58fbc + 0dc9a18 commit ded9641

File tree

4 files changed

+30
-0
lines changed

4 files changed

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

lib/rubocop-performance.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@
99
RuboCop::Performance::Inject.defaults!
1010

1111
require_relative 'rubocop/cop/performance_cops'
12+
13+
RuboCop::Cop::Lint::UnusedMethodArgument.singleton_class.prepend(
14+
Module.new do
15+
def autocorrect_incompatible_with
16+
super.push(RuboCop::Cop::Performance::BlockGivenWithExplicitBlock)
17+
end
18+
end
19+
)

lib/rubocop/cop/performance/block_given_with_explicit_block.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def on_send(node)
4747
corrector.replace(node, block_arg_name)
4848
end
4949
end
50+
51+
def self.autocorrect_incompatible_with
52+
[Lint::UnusedMethodArgument]
53+
end
5054
end
5155
end
5256
end

spec/rubocop/cli/autocorrect_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@
3030
RUBY
3131
end
3232

33+
it 'corrects `Performance/BlockGivenWithExplicitBlock` with `Lint/UnusedMethodArgument`' do
34+
source = <<~RUBY
35+
def foo(&block)
36+
block_given?
37+
end
38+
RUBY
39+
create_file('example.rb', source)
40+
expect(
41+
cli.run(['--autocorrect', '--only', 'Performance/BlockGivenWithExplicitBlock,Lint/UnusedMethodArgument'])
42+
).to eq(0)
43+
expect(File.read('example.rb')).to eq(<<~RUBY)
44+
def foo()
45+
block_given?
46+
end
47+
RUBY
48+
end
49+
3350
private
3451

3552
def create_file(file_path, content)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy