Skip to content
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

Invalid autocorrection of ternary expression by RedundantDoubleSplatHashBraces #12286

Closed
nevans opened this issue Oct 16, 2023 · 0 comments · Fixed by #12288
Closed

Invalid autocorrection of ternary expression by RedundantDoubleSplatHashBraces #12286

nevans opened this issue Oct 16, 2023 · 0 comments · Fixed by #12288
Labels

Comments

@nevans
Copy link
Contributor

nevans commented Oct 16, 2023

rubocop 1.57.1 incorrectly identifies and incorrectly autocorrects the following code:

# frozen_string_literal: true

foo(**(bar ? {bar:} : {}))
foo(bar:, **(baz ? {baz:} : {}))

My apologies if this was already fixed by #12278, but I don't think it is (and it doesn't match any of the test cases there). It's related to #12278, at any rate!


Expected behavior

It should not report an offense, the double splat is not redundant in these examples. And it especially should not autocorrect to invalid syntax.

Actual behavior

$ bin/rubocop tmp/rubocop-example.rb -A
Inspecting 1 file
F

Offenses:

tmp/rubocop-example.rb:3:5: C: [Corrected] Style/RedundantDoubleSplatHashBraces: Remove the redundant double splat and b
races, use keyword arguments directly.
foo(**(bar ? {bar:} : {}))
    ^^^^^^^^^^^^^^^^^^^^^
tmp/rubocop-example.rb:3:17: F: Lint/Syntax: unexpected token tCOLON
(Using Ruby 3.1 parser; configure using TargetRubyVersion parameter, under AllCops)
foo((bar ? bar: : {}))
                ^
tmp/rubocop-example.rb:4:11: C: [Corrected] Style/RedundantDoubleSplatHashBraces: Remove the redundant double splat and 
braces, use keyword arguments directly.
foo(bar:, **(baz ? {baz:} : {}))
          ^^^^^^^^^^^^^^^^^^^^^
tmp/rubocop-example.rb:4:23: F: Lint/Syntax: unexpected token tCOLON
(Using Ruby 3.1 parser; configure using TargetRubyVersion parameter, under AllCops)
foo(bar:, (baz ? baz: : {}))
                      ^
tmp/rubocop-example.rb:4:28: F: Lint/Syntax: unexpected token tRPAREN
(Using Ruby 3.1 parser; configure using TargetRubyVersion parameter, under AllCops)
foo(bar:, (baz ? baz: : {}))
                           ^

1 file inspected, 5 offenses detected, 2 offenses corrected

After autocorrection:

# frozen_string_literal: true

foo((bar ? bar: : {}))
foo(bar:, (baz ? baz: : {}))

Steps to reproduce the problem

See above.

RuboCop version

$ rubocop -V
1.57.1 (using Parser 3.2.2.4, rubocop-ast 1.29.0, running on ruby 3.1.4) [x86_64-linux]
  - rubocop-capybara 2.19.0
  - rubocop-factory_bot 2.24.0
  - rubocop-performance 1.19.1
  - rubocop-rails 2.21.2
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.24.1
@koic koic added the bug label Oct 17, 2023
koic added a commit to koic/rubocop that referenced this issue Oct 17, 2023
…atHashBraces`

Fixes rubocop#12286.

This PR fix false positives for `Style/RedundantDoubleSplatHashBraces`
when using double splat with a hash literal enclosed in parenthesized ternary operator.
koic added a commit that referenced this issue Oct 17, 2023
…ndant_double_splat_hash_braces

[Fix #12286] Fix false positives for `Style/RedundantDoubleSplatHashBraces`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants