Skip to content

Commit

Permalink
[Fix #12361] Fix an incorrect autocorrect for Naming/BlockForwarding
Browse files Browse the repository at this point in the history
Fixes #12361.

Fix an incorrect autocorrect for `Naming/BlockForwarding` and `Style/ArgumentsForwarding`
when autocorrection conflicts for anonymous arguments.
  • Loading branch information
koic authored and bbatsov committed Nov 5, 2023
1 parent d3de2ae commit 8579181
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12361](https://github.com/rubocop/rubocop/issues/12361): Fix an incorrect autocorrect for `Naming/BlockForwarding` and `Style/ArgumentsForwarding` when autocorrection conflicts for anonymous arguments. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/naming/block_forwarding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class BlockForwarding < Base
MSG = 'Use %<style>s block forwarding.'

def self.autocorrect_incompatible_with
[Lint::AmbiguousOperator]
[Lint::AmbiguousOperator, Style::ArgumentsForwarding]
end

def on_def(node)
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/arguments_forwarding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class ArgumentsForwarding < Base
ARGS_MSG = 'Use anonymous positional arguments forwarding (`*`).'
KWARGS_MSG = 'Use anonymous keyword arguments forwarding (`**`).'

def self.autocorrect_incompatible_with
[Naming::BlockForwarding]
end

def on_def(node)
return unless node.body

Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,28 @@ def foo(options, &)
RUBY
end

it 'corrects `Naming/BlockForwarding` with `Style/ArgumentsForwarding`' do
create_file('.rubocop.yml', <<~YAML)
AllCops:
TargetRubyVersion: 3.2
YAML
source = <<~RUBY
def some_method(form, **options, &block)
render 'template', form: form, **options, &block
end
RUBY
create_file('example.rb', source)
expect(cli.run([
'--autocorrect',
'--only', 'Naming/BlockForwarding,Style/ArgumentsForwarding'
])).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
def some_method(form, **, &)
render('template', form: form, **, &)
end
RUBY
end

describe 'trailing comma cops' do
let(:source) do
<<~RUBY
Expand Down

0 comments on commit 8579181

Please sign in to comment.