Skip to content

Commit

Permalink
[Fix #12758] Fix false positives for Layout/RedundantLineBreak
Browse files Browse the repository at this point in the history
Fixes #12758.

This PR fixes false positives for `Layout/RedundantLineBreak`
when using `&&` or `||` after a backslash newline.
  • Loading branch information
koic authored and bbatsov committed Mar 9, 2024
1 parent 5a4664c commit 890e225
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12758](https://github.com/rubocop/rubocop/issues/12758): Fix false positives for `Layout/RedundantLineBreak` when using `&&` or `||` after a backslash newline. ([@koic][])
10 changes: 8 additions & 2 deletions lib/rubocop/cop/layout/redundant_line_break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ def register_offense(node)
end

def offense?(node)
node.multiline? && !too_long?(node) && suitable_as_single_line?(node) &&
!index_access_call_chained?(node) && !configured_to_not_be_inspected?(node)
return false if !node.multiline? || too_long?(node) || !suitable_as_single_line?(node)
return require_backslash?(node) if node.and_type? || node.or_type?

!index_access_call_chained?(node) && !configured_to_not_be_inspected?(node)
end

def require_backslash?(node)
processed_source.lines[node.loc.operator.line - 1].end_with?('\\')
end

def index_access_call_chained?(node)
Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/cop/layout/redundant_line_break_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,44 @@
RUBY
end

it 'registers an offense when using `&&` before a backslash newline' do
expect_offense(<<~RUBY)
foo && \\
^^^^^^^^ Redundant line break detected.
bar
RUBY

expect_correction(<<~RUBY)
foo && bar
RUBY
end

it 'does not register an offense when using `&&` after a backslash newline' do
expect_no_offenses(<<~RUBY)
foo \\
&& bar
RUBY
end

it 'registers an offense when using `||` before a backslash newline' do
expect_offense(<<~RUBY)
foo || \\
^^^^^^^^ Redundant line break detected.
bar
RUBY

expect_correction(<<~RUBY)
foo || bar
RUBY
end

it 'does not register an offense when using `||` after a backslash newline' do
expect_no_offenses(<<~RUBY)
foo \\
|| bar
RUBY
end

context 'with LineLength Max 100' do
let(:max_line_length) { 100 }

Expand Down

0 comments on commit 890e225

Please sign in to comment.