Skip to content

Commit f94cd24

Browse files
jonas054bbatsov
authored andcommitted
[Fix rubocop#10311] Expand send nodes correctly
When Layout/RedundantLineBreak inspects a send node, it starts by expanding the node to inspect. It does this by traversing the AST upwards as long as the parent is a send node of a block node. The problem was that if the inspected node is a single node inside a do..end block, we shouldn't move to the parent block and inspect it for redundant line breaks. It's only send nodes before the do..end that we want to expand in order to inspect "the whole expression". So we should check what kind of block/send relationship it is before moving upwards in the AST.
1 parent c8dcda8 commit f94cd24

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#10311](https://github.com/rubocop/rubocop/issues/10311): Fix false negative inside `do`..`end` for `Layout/RedundantLineBreak`. ([@jonas054][])

lib/rubocop/cop/layout/redundant_line_break.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ def suitable_as_single_line?(node)
105105
end
106106

107107
def convertible_block?(node)
108-
return false unless node.parent&.block_type?
109-
110-
send_node = node.parent&.send_node
111-
send_node.parenthesized? || !send_node.arguments?
108+
parent = node.parent
109+
parent&.block_type? && node == parent.send_node &&
110+
(node.parenthesized? || !node.arguments?)
112111
end
113112

114113
def comment_within?(node)

spec/rubocop/cop/layout/redundant_line_break_spec.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def resolve_inheritance_from_gems(hash)
131131
^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
132132
"can't inherit configuration from the rubocop gem"
133133
end
134-
#{' '}
134+
135135
hash['inherit_from'] = Array(hash['inherit_from'])
136136
Array(config_path).reverse_each do |path|
137137
# Put gem configuration first so local configuration overrides it.
@@ -148,7 +148,7 @@ def resolve_inheritance_from_gems(hash)
148148
if gem_name == 'rubocop'
149149
raise ArgumentError, "can't inherit configuration from the rubocop gem"
150150
end
151-
#{' '}
151+
152152
hash['inherit_from'] = Array(hash['inherit_from'])
153153
Array(config_path).reverse_each do |path|
154154
# Put gem configuration first so local configuration overrides it.
@@ -173,6 +173,23 @@ def resolve_inheritance_from_gems(hash)
173173
RUBY
174174
end
175175

176+
it 'registers an offense for a method call on multiple lines inside a block' do
177+
expect_offense(<<~RUBY)
178+
some_array.map do |something|
179+
my_method(
180+
^^^^^^^^^^ Redundant line break detected.
181+
something,
182+
)
183+
end
184+
RUBY
185+
186+
expect_correction(<<~RUBY)
187+
some_array.map do |something|
188+
my_method( something, )
189+
end
190+
RUBY
191+
end
192+
176193
it 'accepts a method call on multiple lines if there are comments on them' do
177194
expect_no_offenses(<<~RUBY)
178195
my_method(1,

0 commit comments

Comments
 (0)