Skip to content

Fix Style/RedundantParentheses pattern matching OR condition #14275

@bkuhlmann

Description

@bkuhlmann

Expected behavior

I would expect the following to be valid (and require the use of parentheses due to the OR condition):

def only_double_splats?(parameters) = (parameters in [[:keyrest]] | [[:keyrest, *]])

Actual behavior

I'll get the following:

lib/marameters/probe.rb:60:31: C: [Correctable] Style/RedundantParentheses: Don't use parentheses around a one-line pattern matching.
    def only_double_splats? = (parameters in [[:keyrest]] | [[:keyrest, *]])
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If I use auto-correct, the above will go way but then my specs will fail because the OR condition (i.e. [[:keyrest, *]]) will not be respected.

Steps to reproduce the problem

Use the following code:

def only_double_splats?(parameters) = parameters in [[:keyrest]] | [[:keyrest, *]]

parameters = Module.new { def test(**one) = super }
                   .instance_method(:test)
                   .parameters

only_double_splats?(parameters) == true # false

The problem with the above is that this is returned instead:

[
  [
    :keyrest,
    :one
  ]
]

...which is definitely not true.

RuboCop version

1.76.1 (using Parser 3.3.8.0, Prism 1.4.0, rubocop-ast 1.45.1, analyzing as Ruby 3.4, running on ruby 3.4.4) [arm64-darwin24.5.0]
  - rubocop-capybara 2.22.1
  - rubocop-disable_syntax 0.2.0
  - rubocop-packaging 0.6.0
  - rubocop-performance 1.25.0
  - rubocop-thread_safety 0.7.2
  - rubocop-rake 0.7.1
  - rubocop-rspec 3.6.0

Notes

This is a follow up to Issue #14239 where I mentioned this was a syntax error but I was incorrect. This is a logic error, instead, which will cause your specs to fail if you drop the parentheses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions