Skip to content

Commit f8aa27f

Browse files
koicbbatsov
authored andcommitted
Fix a false negative for Style/RedundantLineContinuation
This PR fixes a false negative for `Style/RedundantLineContinuation` when there is a line continuation at the EOF.
1 parent d033a5e commit f8aa27f

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#13401](https://github.com/rubocop/rubocop/pull/13401): Fix a false negative for `Style/RedundantLineContinuation` when there is a line continuation at the EOF. ([@koic][])

lib/rubocop/cop/style/redundant_line_continuation.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class RedundantLineContinuation < Base
6969
extend AutoCorrector
7070

7171
MSG = 'Redundant line continuation.'
72+
LINE_CONTINUATION = "\\\n"
73+
LINE_CONTINUATION_PATTERN = /(\\\n)/.freeze
7274
ALLOWED_STRING_TOKENS = %i[tSTRING tSTRING_CONTENT].freeze
7375
ARGUMENT_TYPES = %i[
7476
kFALSE kNIL kSELF kTRUE tCONSTANT tCVAR tFLOAT tGVAR tIDENTIFIER tINTEGER tIVAR
@@ -79,14 +81,16 @@ class RedundantLineContinuation < Base
7981
def on_new_investigation
8082
return unless processed_source.ast
8183

82-
each_match_range(processed_source.ast.source_range, /(\\\n)/) do |range|
84+
each_match_range(processed_source.ast.source_range, LINE_CONTINUATION_PATTERN) do |range|
8385
next if require_line_continuation?(range)
8486
next unless redundant_line_continuation?(range)
8587

8688
add_offense(range) do |corrector|
8789
corrector.remove_leading(range, 1)
8890
end
8991
end
92+
93+
inspect_eof_line_continuation
9094
end
9195

9296
private
@@ -132,6 +136,17 @@ def redundant_line_continuation?(range)
132136
parse(source.gsub("\\\n", "\n")).valid_syntax?
133137
end
134138

139+
def inspect_eof_line_continuation
140+
return unless processed_source.raw_source.end_with?(LINE_CONTINUATION)
141+
142+
rindex = processed_source.raw_source.rindex(LINE_CONTINUATION)
143+
line_continuation_range = range_between(rindex, rindex + 1)
144+
145+
add_offense(line_continuation_range) do |corrector|
146+
corrector.remove_trailing(line_continuation_range, 1)
147+
end
148+
end
149+
135150
def inside_string_literal?(range, token)
136151
ALLOWED_STRING_TOKENS.include?(token.type) && token.pos.overlaps?(range)
137152
end

spec/rubocop/cop/style/redundant_line_continuation_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,4 +707,15 @@ def do_something
707707
baz)
708708
RUBY
709709
end
710+
711+
it 'registers an offense when there is a line continuation at the EOF' do
712+
expect_offense(<<~'RUBY')
713+
foo \
714+
^ Redundant line continuation.
715+
RUBY
716+
717+
expect_correction(<<~RUBY)
718+
foo#{trailing_whitespace}
719+
RUBY
720+
end
710721
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy