From 2821564bdbaf1d4e08de802f05cf9b50a479cd53 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 25 Apr 2024 02:54:20 +0900 Subject: [PATCH] [Fix #12862] Fix a false positive for `Style/RedundantLineContinuation` Fixes #12862. This PR fixes a false positive for `Style/RedundantLineContinuation` when line continuations involve `return` with a return value. --- ...e_for_style_redundant_line_continuation.md | 1 + .../cop/style/redundant_line_continuation.rb | 4 +++- .../style/redundant_line_continuation_spec.rb | 22 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_false_positive_for_style_redundant_line_continuation.md diff --git a/changelog/fix_false_positive_for_style_redundant_line_continuation.md b/changelog/fix_false_positive_for_style_redundant_line_continuation.md new file mode 100644 index 00000000000..f5788bcc4b1 --- /dev/null +++ b/changelog/fix_false_positive_for_style_redundant_line_continuation.md @@ -0,0 +1 @@ +* [#12862](https://github.com/rubocop/rubocop/issues/12862): Fix a false positive for `Style/RedundantLineContinuation` when line continuations involve `return` with a return value. ([@koic][]) diff --git a/lib/rubocop/cop/style/redundant_line_continuation.rb b/lib/rubocop/cop/style/redundant_line_continuation.rb index 7261f0a5eae..93a1126d831 100644 --- a/lib/rubocop/cop/style/redundant_line_continuation.rb +++ b/lib/rubocop/cop/style/redundant_line_continuation.rb @@ -137,7 +137,9 @@ def inside_string_literal?(range, token) # do_something \ # argument def method_with_argument?(current_token, next_token) - current_token.type == :tIDENTIFIER && ARGUMENT_TYPES.include?(next_token.type) + return false if current_token.type != :tIDENTIFIER && current_token.type != :kRETURN + + ARGUMENT_TYPES.include?(next_token.type) end # rubocop:disable Metrics/AbcSize diff --git a/spec/rubocop/cop/style/redundant_line_continuation_spec.rb b/spec/rubocop/cop/style/redundant_line_continuation_spec.rb index c9b62c681cf..7f14a432ff2 100644 --- a/spec/rubocop/cop/style/redundant_line_continuation_spec.rb +++ b/spec/rubocop/cop/style/redundant_line_continuation_spec.rb @@ -114,6 +114,28 @@ def self.foo(bar,#{trailing_whitespace} RUBY end + it 'does not register an offense when line continuations involve `return` with a return value' do + expect_no_offenses(<<~'RUBY') + return \ + foo + RUBY + end + + it 'registers an offense when line continuations involve `return` with a parenthesized return value' do + expect_offense(<<~'RUBY') + return(\ + ^ Redundant line continuation. + foo + ) + RUBY + + expect_correction(<<~RUBY) + return( + foo + ) + RUBY + end + it 'registers an offense when line continuations with `if`' do expect_offense(<<~'RUBY') if foo \