Skip to content

Commit a1a7c35

Browse files
committed
Fix Performance/CaseWhenSplat cop error on when node without body
Previously if `when` node has an empty body this cop raises: ```ruby case 1 when 1 # no body # 1 == 1 else nil end ``` ``` undefined method `loc' for nil # ./lib/rubocop/cop/performance/case_when_splat.rb:154:in `indent_for' # ./lib/rubocop/cop/performance/case_when_splat.rb:144:in `new_branch_without_then' # ./lib/rubocop/cop/performance/case_when_splat.rb:123:in `reordering_correction' # ./lib/rubocop/cop/performance/case_when_splat.rb:114:in `reorder_condition' # ./lib/rubocop/cop/performance/case_when_splat.rb:86:in `autocorrect' ``` It also did not preserve comments.
1 parent 4e147b0 commit a1a7c35

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#484](https://github.com/rubocop/rubocop-performance/pull/484): Fix `Performance/CaseWhenSplat` cop error on `when` node without body. ([@viralpraxis][])

lib/rubocop/cop/performance/case_when_splat.rb

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ module Performance
5858
class CaseWhenSplat < Base
5959
include Alignment
6060
include RangeHelp
61+
include CommentsHelp
6162
extend AutoCorrector
6263

6364
MSG = 'Reordering `when` conditions with a splat to the end of the `when` branches can improve performance.'
@@ -116,11 +117,18 @@ def reorder_condition(corrector, when_node)
116117
def reordering_correction(when_node)
117118
new_condition = replacement(when_node.conditions)
118119

119-
if same_line?(when_node, when_node.body)
120-
new_condition_with_then(when_node, new_condition)
121-
else
122-
new_branch_without_then(when_node, new_condition)
123-
end
120+
condition =
121+
if same_line?(when_node, when_node.body)
122+
new_condition_with_then(when_node, new_condition)
123+
else
124+
new_branch_without_then(when_node, new_condition)
125+
end
126+
127+
condition_comments = comments_in_range(when_node).map do |comment_node|
128+
"#{indent_for(comment_node)}#{comment_node.source}"
129+
end.join("\n")
130+
131+
"#{condition}#{condition_comments}"
124132
end
125133

126134
def when_branch_range(when_node)
@@ -134,7 +142,13 @@ def new_condition_with_then(node, new_condition)
134142
end
135143

136144
def new_branch_without_then(node, new_condition)
137-
"\n#{indent_for(node)}when #{new_condition}\n#{indent_for(node.body)}#{node.body.source}"
145+
new_branch = "\n#{indent_for(node)}when #{new_condition}\n"
146+
147+
if node.body
148+
"#{new_branch}#{indent_for(node.body)}#{node.body.source}"
149+
else
150+
new_branch
151+
end
138152
end
139153

140154
def indent_for(node)

rubocop-performance-1.23.0.gem

43.5 KB
Binary file not shown.

spec/rubocop/cop/performance/case_when_splat_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@
5454
RUBY
5555
end
5656

57+
it 'registers an offense for case when with a splat in the first condition with branch without body' do
58+
expect_offense(<<~RUBY)
59+
case foo
60+
when *cond
61+
^^^^^^^^^^ Reordering `when` conditions with a splat to the end of the `when` branches can improve performance.
62+
# bar
63+
# bar
64+
# bar
65+
when 3
66+
baz
67+
end
68+
RUBY
69+
70+
expect_correction(<<~RUBY)
71+
case foo
72+
when 3
73+
baz
74+
when *cond
75+
# bar
76+
# bar
77+
# bar
78+
end
79+
RUBY
80+
end
81+
5782
it 'registers an offense for case when with a splat without an else' do
5883
expect_offense(<<~RUBY)
5984
case foo

0 commit comments

Comments
 (0)