Skip to content

Commit

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

This PR fixes a false positive for `Style/RedundantLineContinuation` when
using line concatenation for assigning a return value and without argument parentheses.
  • Loading branch information
koic authored and bbatsov committed May 4, 2023
1 parent 23e80f2 commit 799f698
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11844](https://github.com/rubocop/rubocop/issues/11844): Fix a false positive for `Style/RedundantLineContinuation` when using line concatenation for assigning a return value and without argument parentheses. ([@koic][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/style/redundant_line_continuation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class RedundantLineContinuation < Base

MSG = 'Redundant line continuation.'
ALLOWED_STRING_TOKENS = %i[tSTRING tSTRING_CONTENT].freeze
ARGUMENT_TYPES = %i[
kFALSE kNIL kSELF kTRUE tCONSTANT tCVAR tFLOAT tGVAR tIDENTIFIER tINTEGER tIVAR
tLBRACK tLCURLY tLPAREN_ARG tSTRING tSTRING_BEG tSYMBOL tXSTRING_BEG
].freeze

def on_new_investigation
return unless processed_source.ast
Expand Down Expand Up @@ -124,7 +128,7 @@ def inside_string_literal?(range, token)
# do_something \
# argument
def method_with_argument?(current_token, next_token)
current_token.type == :tIDENTIFIER && next_token.type == :tIDENTIFIER
current_token.type == :tIDENTIFIER && ARGUMENT_TYPES.include?(next_token.type)
end

def argument_newline?(node)
Expand Down
130 changes: 129 additions & 1 deletion spec/rubocop/cop/style/redundant_line_continuation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,141 @@ 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
it 'does not register an offense when using line concatenation for assigning a return value and without argument parentheses of method call' do
expect_no_offenses(<<~'RUBY')
foo = do_something \
argument
RUBY
end

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

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

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

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

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

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

it 'does not register an offense when using line concatenation for assigning a return value and without argument parentheses of interpolated string literal' do
expect_no_offenses(<<~'RUBY')
foo = do_something \
"argument#{x}"
RUBY
end

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

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

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

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

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

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

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

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

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

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

it 'does not register an offense when using line concatenation for assigning a return value and without argument parentheses of hash literal' do
expect_no_offenses(<<~'RUBY')
foo = do_something \
{}
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 799f698

Please sign in to comment.