diff --git a/changelog/fix_false_positive_for_style_redundant_return.md b/changelog/fix_false_positive_for_style_redundant_return.md new file mode 100644 index 00000000000..7ed400a1355 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/style/redundant_return.rb b/lib/rubocop/cop/style/redundant_return.rb index 15e393c9945..979da17271b 100644 --- a/lib/rubocop/cop/style/redundant_return.rb +++ b/lib/rubocop/cop/style/redundant_return.rb @@ -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) diff --git a/spec/rubocop/cop/style/redundant_return_spec.rb b/spec/rubocop/cop/style/redundant_return_spec.rb index 7c480c62c8b..8d10aa00d93 100644 --- a/spec/rubocop/cop/style/redundant_return_spec.rb +++ b/spec/rubocop/cop/style/redundant_return_spec.rb @@ -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