-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong Lint/UnneededSplatExpansion #3512
Comments
Same here! In Rails {'one' => 1, 'two' => 2, 'three' => 3}.slice(*%w(one three))
=> {"one"=>1, "three"=>3} Rubocop tells me W: Unnecessary splat expansion.
{'one' => 1, 'two' => 2, 'three' => 3}.slice(*%w(one three)) Which is wrong, because the result is not the same. {'one' => 1, 'two' => 2, 'three' => 3}.slice(%w(one three))
=> {} $ rubocop -V
0.43.0 (using Parser 2.3.1.3, running on ruby 2.3.0 x86_64-darwin15) |
Rubocop is detecting and correcting the offense. This does not seem to be a bug: ⇒ cat rubocop_bug_test.rb
{ 'one' => 1, 'two' => 2, 'three' => 3 }.slice(*%w(one three)) ⇒ rubocop -a rubocop_bug_test.rb
Inspecting 1 file
W
Offenses:
rubocop_bug_test.rb:1:48: W: [Corrected] Unnecessary splat expansion.
{ 'one' => 1, 'two' => 2, 'three' => 3 }.slice(*%w(one three))
^^^^^^^^^^^^^^
1 file inspected, 1 offense detected, 1 offense corrected ⇒ cat rubocop_bug_test.rb
{ 'one' => 1, 'two' => 2, 'three' => 3 }.slice('one', 'three') |
Also false positive for Pathname#join: Rails.root.join(*['a', 'b', 'c']) Please tell me if you would rather like a separate issue. |
@NobodysNightmare I'm not sure what is wrong with your example. It correctly autocorrects to |
Okay I see... Apparently the error message was simply confusing to me. "Unneccessary splat expansion" implies to me, that I do not need to expand the array (i.e. leave the splat operator out). However, what the cop wants to tell me is that I could just have written the parameter list out manually... Right? So it was me misunderstanding the cop, sorry... |
@tejasbubane you're right. I didn't get the obvious change. 😞 |
👍 no problem @NobodysNightmare |
I agree that the message can be misinterpreted. That was also my interpretation, hence my example above wihtout the splat operator but keeping the array. Maybe changing the error copy would be a good thing to do. |
…on` for array In case of array splat for method parameter, the default message was ambiguous. `Unnecessary splat expansion.` made people just remove the splat expansion and pass the array as parameter which is obviously not the same as passing splat arguments. Change the message to `Pass array contents as separate arguments.` to the message in such cases to make the intension more clear.
Regarding the original issue by @YukiJikumaru (with splat inside splat). Rubocop works as expected. original code: validates(
*[:column_A, *OTHER_COLUMNS]
) gets autocorrected to: validates(
:column_A, *OTHER_COLUMNS
) Hence this is not a bug.
|
@tejasbubane Thank you for your answer! |
… array (#3526) In case of array splat for method parameter, the default message was ambiguous. `Unnecessary splat expansion.` made people just remove the splat expansion and pass the array as parameter which is obviously not the same as passing splat arguments. Change the message to `Pass array contents as separate arguments.` to the message in such cases to make the intension more clear.
* bbatsov/master: (80 commits) [Fix rubocop#3540] Make `Style/GuardClause` register offense for instance & singleton methods [Fix rubocop#3436] issue related to Rails/SaveBang when returning non-bang call from the parent method Allow `#to_yml` to produce single-quoted strings Add support for StyleGuideBaseURL and update rules Add spec for the existing style guide URL implementation Fix the changelog Edited regular expression for normal case to fix issues rubocop#3514 and rubocop#3516 (rubocop#3524) Add a rake task for generation a new cop (rubocop#3533) [Fix rubocop#3510] Various bug fixes for SafeNavigation (rubocop#3517) [Fix rubocop#3512] Change error message of `Lint/UnneededSplatExpansion` for array (rubocop#3526) Fix false positive in `Lint/AssignmentInCondition` (rubocop#3520) (rubocop#3529) Rename a mismatched filename (rubocop#3523) Fix a broken changelog entry [Fix rubocop#3511] Style/TernaryParentheses false positive (rubocop#3513) Fix the release notes for 0.43 Cut 0.43.0 [Fix rubocop#3462] Don't flag single-line methods Fix false negatives in `Rails/NotNullColumn` cop (rubocop#3508) Remove unused doubled methods (rubocop#3509) [Fix rubocop#3485] Make OneLineConditional cop ignore empty else (rubocop#3487) ...
…on` for array (rubocop#3526) In case of array splat for method parameter, the default message was ambiguous. `Unnecessary splat expansion.` made people just remove the splat expansion and pass the array as parameter which is obviously not the same as passing splat arguments. Change the message to `Pass array contents as separate arguments.` to the message in such cases to make the intension more clear.
It seems false positive here: Rails.root.join(*%w(db seeders users.rb)) Rubocop complaints on this:
I know I could get rid of this warning with: Rails.root.join('db', 'seeders', 'users.rb')
|
@samnang That is the desired behaviour: https://github.com/bbatsov/rubocop/blob/master/spec/rubocop/cop/lint/unneeded_splat_expansion_spec.rb#L57-L64 |
Expected behavior
$ rubocop hoge.rb
This should not generate Lint/UnneededSplatExpansion offenses.
Actual behavior
If modify like below, offense stops.
However this code produces NoMethodError.
Steps to reproduce the problem
RuboCop version
The text was updated successfully, but these errors were encountered: