From 978578a677d5a9aa6dec439e301d7bc563a4de0e Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 23 Mar 2021 01:31:11 +0900 Subject: [PATCH] Fix an incorrect auto-correct for `Style/RedundantReturn` Fixes https://github.com/testdouble/standard/issues/277. This PR fixes the following false positive for `Style/RedundantReturn` when using `return` with splat argument. ```console % cat example.rb class MyClass def something data = [1, 2, 3] return *data end end ``` ## Before Auto-corrected to invalid code. ```console % rubocop --only Style/RedundantReturn -a (snip) % cat example.rb class MyClass def something data = [1, 2, 3] *data end end % ruby -c example.rb example.rb:4: syntax error, unexpected '\n', expecting '=' ``` ## After Auto-corrected to valid code. ```console % rubocop --only Style/RedundantReturn -a (snip) % cat example.rb class MyClass def something data = [1, 2, 3] data end end % ruby -c example.rb Syntax OK ``` --- ...false_positive_for_style_redundant_return.md | 1 + lib/rubocop/cop/style/redundant_return.rb | 4 ++++ spec/rubocop/cop/style/redundant_return_spec.rb | 17 +++++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 changelog/fix_false_positive_for_style_redundant_return.md 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