Rubocop: False positive for Lint/UnneededSplatExpansion

Created on 1 Aug 2018  路  5Comments  路  Source: rubocop-hq/rubocop

While playing around I found this false positive. Admittedly this probably isn't code you would find in the wild a lot, but I was just playing around in the Ruby console when I found this.

Expected behavior

RuboCop should correctly recognize this use of the splat operator.

Actual behavior

RuboCop detects a Lint/UnneededSplatExpansion violation which, if followed, results in code that doesn't work as expected.

Steps to reproduce the problem

# splat_rubocop.rb

status = :halt

case status
when %i[halt stop]
  pp 'Not working'
end

case status
when *%i[halt stop]
  pp 'Working'
end
$ ruby splat_rubocop.rb
"Working"
$ rubocop splat_rubocop.rb
Inspecting 1 file
W

Offenses:

splat_rubocop.rb:9:6: W: Lint/UnneededSplatExpansion: Unnecessary splat expansion.
when *%i[halt stop]
     ^^^^^^^^^^^^^^

1 file inspected, 1 offense detected



md5-250807613494f80a6970f555ec646f2a



$ rubocop -V
0.58.2 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-darwin16)

Most helpful comment

What would have been more clear in your opinion?

Hmm, I'm not sure what cases this cop covers, but in my case maybe instead of just _"Unnecessary splat expansion."_ something with the necessary action as well _"Unnecessary splat expansion. Use comma separated values instead."_.

What do you think?

All 5 comments

@aried3r Thanks for the feedback.
This looks like it behaves as expected. The code that auto-corrected behaves as follows.

% rubocop -V
0.58.2 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-darwin17)

% rubocop splat_rubocop.rb --only Lint/UnneededSplatExpansion -a
Inspecting 1 file
W

Offenses:

splat_rubocop.rb:9:6: W: [Corrected] Lint/UnneededSplatExpansion: Unnecessary splat expansion.
when *%i[halt stop]
     ^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

% cat splat_rubocop.rb
status = :halt

case status
when %i[halt stop]
  pp 'Not working'
end

case status
when :halt, :stop
  pp 'Working'
end

% ruby splat_rubocop.rb
"Working"

The difference due to auto-correct is as follows.

 status = :halt

 case status
 when %i[halt stop]
   pp 'Not working'
 end

 case status
-when *%i[halt stop]
+when :halt, :stop
   pp 'Working'
 end

I think there is not any problem. How is this?

I think there is not any problem. How is this?

You're right, I didn't use the auto correct feature and misinterpreted what the cop was trying to tell me. I thought the cop meant to just remove the * instead of comma-separating the values myself.

Indeed there's nothing wrong, thanks!

@aried3r I agree that the message isn't clear. What would have been more clear in your opinion?

What would have been more clear in your opinion?

Hmm, I'm not sure what cases this cop covers, but in my case maybe instead of just _"Unnecessary splat expansion."_ something with the necessary action as well _"Unnecessary splat expansion. Use comma separated values instead."_.

What do you think?

That sounds good to me. I opened a new issue if you or anybody else wants to help out with that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tedPen picture tedPen  路  3Comments

lepieru picture lepieru  路  3Comments

AndreiMotinga picture AndreiMotinga  路  3Comments

kirrmann picture kirrmann  路  3Comments

bquorning picture bquorning  路  3Comments