Skip to content

Style/RedundantParentheses still breaks pattern matching #14292

@akimd

Description

@akimd

1.76.1 fixed some of the issues, but it is still dangerous to trust it on pattern matching.

Expected behavior

$ cat rubocop/redundant_parens_2.rb
# frozen_string_literal: true
lhs = {foo: 1}
res = (lhs in {foo:})
puts res
$ ruby rubocop/redundant_parens_2.rb
true
$ bundle exec rubocop -A rubocop/redundant_parens_2.rb
Inspecting 1 file
C

Offenses:

rubocop/redundant_parens_2.rb:2:1: C: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
lhs = {foo: 1}
^
rubocop/redundant_parens_2.rb:3:7: C: [Corrected] Style/RedundantParentheses: Don't use parentheses around a one-line pattern matching.
res = (lhs in {foo:})
      ^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses corrected
$ cat rubocop/redundant_parens_2.rb
# frozen_string_literal: true

lhs = {foo: 1}
res = lhs in {foo:}
puts res
$ ruby rubocop/redundant_parens_2.rb
{:foo=>1}

RuboCop version

$ bundle exec rubocop -V
1.76.1 (using Parser 3.3.8.0, Prism 1.4.0, rubocop-ast 1.45.1, analyzing as Ruby 3.3, running on ruby 3.3.8) [arm64-darwin24]
  - rubocop-minitest 0.38.1
  - rubocop-performance 1.25.0

Cheers!

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