Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #444] Fix an incorrect autocorrect for `Performance/BlockGivenWi… #445

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines -13 to +24
Copy link
Author

@a-lavis a-lavis Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to let me know (or commit directly to this PR) if you'd prefer to do this a different way.

)
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