Skip to content

Commit

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

This PR fixes false positives for `Style/RedundantLineContinuation`
when using line continuations with `&&` or `||` operator in assignment.
  • Loading branch information
koic authored and bbatsov committed Mar 20, 2024
1 parent 2feef9d commit 3cc5ead
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12797](https://github.com/rubocop/rubocop/issues/12797): Fix false positives for `Style/RedundantLineContinuation` when using line continuations with `&&` or `||` operator in assignment. ([@koic][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/style/redundant_line_continuation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ def redundant_line_continuation?(range)
return true unless (node = find_node_for_line(range.line))
return false if argument_newline?(node)

continuation_node = node.parent || node
continuation_node = node.assignment? ? node.expression : (node.parent || node)
return false if allowed_type?(node) || allowed_type?(continuation_node)

continuation_node.source.include?("\n") || continuation_node.source.include?("\\\n")
continuation_node.source.match?(/(\n|\\\n")/)
end

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

it 'does not register an offense when line continuations with `&&` in assignments' do
expect_no_offenses(<<~'RUBY')
foo = bar\
&& baz
RUBY
end

it 'does not register an offense when line continuations with `||` in assignments' do
expect_no_offenses(<<~'RUBY')
foo = bar\
|| baz
RUBY
end

it 'does not register an offense when line continuations with `&&` in method definition' do
expect_no_offenses(<<~'RUBY')
def do_something
Expand Down

0 comments on commit 3cc5ead

Please sign in to comment.