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

Style/RedundantParentheses autocorrects code into invalid Ruby #12298

Closed
a-lavis opened this issue Oct 19, 2023 · 0 comments · Fixed by #12299
Closed

Style/RedundantParentheses autocorrects code into invalid Ruby #12298

a-lavis opened this issue Oct 19, 2023 · 0 comments · Fixed by #12299
Labels

Comments

@a-lavis
Copy link

a-lavis commented Oct 19, 2023

Style/RedundantParentheses can sometimes autocorrect code into invalid Ruby.
This can happen when passing in an argument to a function that consists of calling a method on a hash.


Expected behavior

RuboCop should not autocorrect in this case. I also think the Style/RedundantParentheses should probably ignore this scenario as well.

Actual behavior

It autocorrects this case, and thus produced invalid Ruby (which RuboCop cannot parse).

When running rubocop -A --debug test.rb (see the below example from the "Steps to reproduce the problem" section), I see:

For /Users/aidanlavis: Default configuration from /Users/aidanlavis/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.57.1/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/aidanlavis/test.rb
Loading cache from /Users/aidanlavis/.cache/rubocop_cache/33ac87dfe8652207bb74821fb2c25f70e384417f/6d7a3b621ca1730e04accd938619e4bdab66cfb1/75f1154f8ccaf01f5993674deb19cb9ba5e3d00c
F

Offenses:

test.rb:3:6: C: [Corrected] Style/RedundantParentheses: Don't use parentheses around a literal.
puts ({ a: 1, b: 2 }).merge({ c: 3 })
     ^^^^^^^^^^^^^^^^
test.rb:3:9: F: Lint/Syntax: unexpected token tCOLON
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
puts { a: 1, b: 2 }.merge({ c: 3 })
        ^

1 file inspected, 2 offenses detected, 1 offense corrected
Finished in 0.08834399999977904 seconds

Steps to reproduce the problem

Create a file test.rb

# frozen_string_literal: true

puts ({ a: 1, b: 2 }).merge({ c: 3 })

You can run this file ruby test.rb and nothing breaks.
If you run rubocop -A test.rb, it autocorrects the file to:

# frozen_string_literal: true

puts { a: 1, b: 2 }.merge({ c: 3 })

This file cannot be parsed - if you try to run it by doing ruby test.rb or if you try to run rubocop on it (rubocop test.rb) it will fail.

RuboCop version

> rubocop -V
1.57.1 (using Parser 3.2.2.4, rubocop-ast 1.29.0, running on ruby 3.2.2) [arm64-darwin22]
@koic koic added the bug label Oct 20, 2023
koic added a commit to koic/rubocop that referenced this issue Oct 20, 2023
…ses`

Fixes rubocop#12298.

This PR fixes a false positive for `Style/RedundantParentheses`
when using a parenthesized hash literal as the first argument
in a method call without parentheses.
koic added a commit that referenced this issue Oct 20, 2023
…dundant_parentheses

[Fix #12298] Fix a false positive for `Style/RedundantParentheses`
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this issue Nov 1, 2023
https://build.opensuse.org/request/show/1121572
by user dancermak + dimstar_suse
- updated to version 1.57.2
  ## 1.57.2 (2023-10-26)
  ### Bug fixes
  * [#12274](rubocop/rubocop#12274): Fix a false positive for `Lint/Void` when `each`'s receiver is an object of `Enumerator` to which `filter` has been applied. ([@koic][])
  * [#12291](rubocop/rubocop#12291): Fix a false positive for `Metrics/ClassLength` when a class with a singleton class definition. ([@koic][])
  * [#12293](rubocop/rubocop#12293): Fix a false positive for `Style/RedundantDoubleSplatHashBraces` when using double splat hash braces with `merge` and method chain. ([@koic][])
  * [#12298](rubocop/rubocop#12298): Fix a false positive for `Style/RedundantParentheses` when using a parenthesized hash literal
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