Skip to content

Commit

Permalink
[Fix #11783] Fix a false positive for Style/RedundantLineContinuation
Browse files Browse the repository at this point in the history
Fixes #11783.

This PR fixes a false positive for `Style/RedundantLineContinuation`
using line concatenation for assigning a return value and without argument parentheses.
  • Loading branch information
koic authored and bbatsov committed Apr 11, 2023
1 parent 121ffc7 commit 2d20ed2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11783](https://github.com/rubocop/rubocop/issues/11783): Fix a false positive for `Style/RedundantLineContinuation` using line concatenation for assigning a return value and without argument parentheses. ([@koic][])
20 changes: 16 additions & 4 deletions lib/rubocop/cop/style/redundant_line_continuation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def require_line_continuation?(range)
!ends_with_backslash_without_comment?(range.source_line) ||
string_concatenation?(range.source_line) ||
start_with_arithmetic_operator?(processed_source[range.line]) ||
inside_string_literal?(range)
inside_string_literal_or_method_with_argument?(range)
end

def ends_with_backslash_without_comment?(source_line)
Expand All @@ -101,9 +101,9 @@ def string_concatenation?(source_line)
/["']\s*\\\z/.match?(source_line)
end

def inside_string_literal?(range)
processed_source.tokens.each.any? do |token|
ALLOWED_STRING_TOKENS.include?(token.type) && token.pos.overlaps?(range)
def inside_string_literal_or_method_with_argument?(range)
processed_source.tokens.each_cons(2).any? do |token, next_token|
inside_string_literal?(range, token) || method_with_argument?(token, next_token)
end
end

Expand All @@ -115,6 +115,18 @@ def redundant_line_continuation?(range)
parse(source.gsub(/\\\n/, "\n")).valid_syntax?
end

def inside_string_literal?(range, token)
ALLOWED_STRING_TOKENS.include?(token.type) && token.pos.overlaps?(range)
end

# A method call without parentheses such as the following cannot remove `\`:
#
# do_something \
# argument
def method_with_argument?(current_token, next_token)
current_token.type == :tIDENTIFIER && next_token.type == :tIDENTIFIER
end

def argument_newline?(node)
node = node.children.first if node.root? && node.begin_type?

Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/style/redundant_line_continuation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ def self.foo(bar,#{trailing_whitespace}
RUBY
end

it 'registers an offense and corrects when using redundant line concatenation for assigning a return value and with argument parentheses' do
expect_offense(<<~'RUBY')
foo = do_something( \
^ Redundant line continuation.
argument)
RUBY

expect_correction(<<~RUBY)
foo = do_something(#{trailing_whitespace}
argument)
RUBY
end

it 'does not register an offense when line continuations for double quoted string' do
expect_no_offenses(<<~'RUBY')
foo = "foo \
Expand Down Expand Up @@ -178,6 +191,13 @@ def self.foo(bar,#{trailing_whitespace}
RUBY
end

it 'does not register an offense when using line concatenation for assigning a return value and without argument parentheses' do
expect_no_offenses(<<~'RUBY')
foo = do_something \
argument
RUBY
end

it 'does not register an offense when line continuations with arithmetic operator' do
expect_no_offenses(<<~'RUBY')
1 \
Expand Down

0 comments on commit 2d20ed2

Please sign in to comment.