diff --git a/changelog/fix_performance_case_when_splat_cop_error_on_when_without_body.md b/changelog/fix_performance_case_when_splat_cop_error_on_when_without_body.md new file mode 100644 index 0000000000..7cd6e80aa1 --- /dev/null +++ b/changelog/fix_performance_case_when_splat_cop_error_on_when_without_body.md @@ -0,0 +1 @@ +* [#484](https://github.com/rubocop/rubocop-performance/pull/484): Fix `Performance/CaseWhenSplat` cop error on `when` node without body. ([@viralpraxis][]) diff --git a/lib/rubocop/cop/performance/case_when_splat.rb b/lib/rubocop/cop/performance/case_when_splat.rb index 0fc30bd660..40c9a565dd 100644 --- a/lib/rubocop/cop/performance/case_when_splat.rb +++ b/lib/rubocop/cop/performance/case_when_splat.rb @@ -58,6 +58,7 @@ module Performance class CaseWhenSplat < Base include Alignment include RangeHelp + include CommentsHelp extend AutoCorrector 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) def reordering_correction(when_node) new_condition = replacement(when_node.conditions) - if same_line?(when_node, when_node.body) - new_condition_with_then(when_node, new_condition) - else - new_branch_without_then(when_node, new_condition) - end + condition = + if same_line?(when_node, when_node.body) + new_condition_with_then(when_node, new_condition) + else + new_branch_without_then(when_node, new_condition) + end + + condition_comments = comments_in_range(when_node).map do |comment_node| + "#{indent_for(comment_node)}#{comment_node.source}" + end.join("\n") + + "#{condition}#{condition_comments}" end def when_branch_range(when_node) @@ -134,7 +142,13 @@ def new_condition_with_then(node, new_condition) end def new_branch_without_then(node, new_condition) - "\n#{indent_for(node)}when #{new_condition}\n#{indent_for(node.body)}#{node.body.source}" + new_branch = "\n#{indent_for(node)}when #{new_condition}\n" + + if node.body + "#{new_branch}#{indent_for(node.body)}#{node.body.source}" + else + new_branch + end end def indent_for(node) diff --git a/rubocop-performance-1.23.0.gem b/rubocop-performance-1.23.0.gem new file mode 100644 index 0000000000..54605d6718 Binary files /dev/null and b/rubocop-performance-1.23.0.gem differ diff --git a/spec/rubocop/cop/performance/case_when_splat_spec.rb b/spec/rubocop/cop/performance/case_when_splat_spec.rb index 4b25112474..99961b053f 100644 --- a/spec/rubocop/cop/performance/case_when_splat_spec.rb +++ b/spec/rubocop/cop/performance/case_when_splat_spec.rb @@ -54,6 +54,31 @@ RUBY end + it 'registers an offense for case when with a splat in the first condition with branch without body' do + expect_offense(<<~RUBY) + case foo + when *cond + ^^^^^^^^^^ Reordering `when` conditions with a splat to the end of the `when` branches can improve performance. + # bar + # bar + # bar + when 3 + baz + end + RUBY + + expect_correction(<<~RUBY) + case foo + when 3 + baz + when *cond + # bar + # bar + # bar + end + RUBY + end + it 'registers an offense for case when with a splat without an else' do expect_offense(<<~RUBY) case foo