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/ParenthesesAroundCondition autocorrects code into invalid Ruby #12529

Closed
a-lavis opened this issue Dec 11, 2023 · 0 comments · Fixed by #12532
Closed

Style/ParenthesesAroundCondition autocorrects code into invalid Ruby #12529

a-lavis opened this issue Dec 11, 2023 · 0 comments · Fixed by #12532
Labels

Comments

@a-lavis
Copy link

a-lavis commented Dec 11, 2023

Style/ParenthesesAroundCondition can sometimes autocorrect code into invalid Ruby.
This seems to happen with multiline while statements.


Expected behavior

RuboCop should not autocorrect in this case. Maybe it should also ignore this kind of scenario?

Actual behavior

It autocorrects this case, and thus produces 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.58.0/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/e41c51a0320816d8293460f2a4dbe0390fc1f056/6d7a3b621ca1730e04accd938619e4bdab66cfb1/abbd0b365a1eddc3b393f5056e799fe44f87a49f
F

Offenses:

test.rb:3:7: C: [Corrected] Style/ParenthesesAroundCondition: Don't use parentheses around the condition of a while.
while (0.upto((rand * 10).ceil).reduce(true) do |try_again, num| ...
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test.rb:3:48: F: Lint/Syntax: unexpected token tPIPE
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
while 0.upto((rand * 10).ceil).reduce(true) do |try_again, num|
                                               ^
test.rb:3:63: F: Lint/Syntax: unexpected token tPIPE
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
while 0.upto((rand * 10).ceil).reduce(true) do |try_again, num|
                                                              ^
test.rb:7:1: F: Lint/Syntax: unexpected token kEND
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
end
^^^

1 file inspected, 4 offenses detected, 1 offense corrected
Finished in 0.09241699999984121 seconds

Steps to reproduce the problem

Create a file test.rb

# frozen_string_literal: true

while (0.upto((rand * 10).ceil).reduce(true) do |try_again, num|
         puts num
         try_again && num < 8
       end)
end

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

while 0.upto((rand * 10).ceil).reduce(true) do |try_again, num|
         puts num
         try_again && num < 8
       end
end

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.58.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 3.2.2) [arm64-darwin22]
@koic koic added the bug label Dec 12, 2023
koic added a commit to koic/rubocop that referenced this issue Dec 12, 2023
…ondition`

Fixes rubocop#12529.

This PR fixes false positives for `Style/ParenthesesAroundCondition` when
using a method call with `do`...`end` block as a condition in a loop.
koic added a commit that referenced this issue Dec 12, 2023
…entheses_around_condition

[Fix #12529] Fix false positives for `Style/ParenthesesAroundCondition`
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