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 an incorrect auto-correct for Style/RedundantReturn #9631

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_false_positive_for_style_redundant_return.md
@@ -0,0 +1 @@
* [#9631](https://github.com/rubocop/rubocop/issues/9631): Fix an incorrect auto-correct for `Style/RedundantReturn` when using `return` with splat argument. ([@koic][])
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/redundant_return.rb
Expand Up @@ -71,6 +71,10 @@ def correct_with_arguments(return_node, corrector)
elsif hash_without_braces?(return_node.first_argument)
add_braces(corrector, return_node.first_argument)
end
if return_node.splat_argument?
first_argument = return_node.first_argument
corrector.replace(first_argument, first_argument.source.gsub(/\A\*/, ''))
end

keyword = range_with_surrounding_space(range: return_node.loc.keyword,
side: :right)
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/style/redundant_return_spec.rb
Expand Up @@ -54,6 +54,23 @@ def func
RUBY
end

it 'reports an offense for def ending with return with splat argument' do
expect_offense(<<~RUBY)
def func
some_preceding_statements
return *something
^^^^^^ Redundant `return` detected.
end
RUBY

expect_correction(<<~RUBY)
def func
some_preceding_statements
something
end
RUBY
end

it 'reports an offense for defs ending with return' do
expect_offense(<<~RUBY)
def self.func
Expand Down