diff --git a/changelog/fix_an_error_for_layout_redundant_line_break.md b/changelog/fix_an_error_for_layout_redundant_line_break.md new file mode 100644 index 000000000000..4f4abce938ac --- /dev/null +++ b/changelog/fix_an_error_for_layout_redundant_line_break.md @@ -0,0 +1 @@ +* [#12050](https://github.com/rubocop/rubocop/pull/12050): Fix a false positive for `Layout/RedundantLineBreak` when inspecting the `%` form string `%\n\n`. ([@koic][]) diff --git a/lib/rubocop/cop/layout/redundant_line_break.rb b/lib/rubocop/cop/layout/redundant_line_break.rb index 6d029e92f8ed..1d1fc907baf5 100644 --- a/lib/rubocop/cop/layout/redundant_line_break.rb +++ b/lib/rubocop/cop/layout/redundant_line_break.rb @@ -48,6 +48,10 @@ class RedundantLineBreak < Base MSG = 'Redundant line break detected.' + def on_lvasgn(node) + super unless end_with_percent_blank_string?(processed_source) + end + def on_send(node) # Include "the whole expression". node = node.parent while node.parent&.send_type? || @@ -61,6 +65,10 @@ def on_send(node) private + def end_with_percent_blank_string?(processed_source) + processed_source.buffer.source.end_with?("%\n\n") + end + def check_assignment(node, _rhs) return unless offense?(node) diff --git a/spec/rubocop/cop/layout/redundant_line_break_spec.rb b/spec/rubocop/cop/layout/redundant_line_break_spec.rb index 6f6d1b20eafc..b9126f70a1e7 100644 --- a/spec/rubocop/cop/layout/redundant_line_break_spec.rb +++ b/spec/rubocop/cop/layout/redundant_line_break_spec.rb @@ -351,6 +351,14 @@ def resolve_inheritance_from_gems(hash) .baz RUBY end + + it 'does not register an offense when the `%` form string `"%\n\n"` at the end of file' do + expect_no_offenses("%\n\n") + end + + it 'does not register an offense when assigning the `%` form string `"%\n\n"` to a variable at the end of file' do + expect_no_offenses("x = %\n\n") + end end end