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

if condition containing semicolon-separated expressions shouldn't have parentheses removed #10227

Closed
FnControlOption opened this issue Nov 1, 2021 · 0 comments · Fixed by #10228
Labels

Comments

@FnControlOption
Copy link
Contributor

The following code prints condition is true:

def foo
  false
end

def bar
  true
end

if (foo; bar)
  puts "condition is true"
else
  puts "condition is false"
end

Output of rubocop foo.rb:

foo.rb:9:4: C: [Correctable] Style/ParenthesesAroundCondition: Don't use parentheses around the condition of an if.
if (foo; bar)
   ^^^^^^^^^^
foo.rb:9:8: C: [Correctable] Style/Semicolon: Do not use semicolons to terminate expressions.
if (foo; bar)
       ^

Expected behavior

RuboCop should not autocorrect.

Actual behavior

The above code gets autocorrected to this:

def foo
  false
end

def bar
  true
end

if foo
  bar
  puts "condition is true"
else
  puts "condition is false"
end

Now the wrong string is printed: condition is false

Output of rubocop --auto-correct foo.rb:

foo.rb:9:4: C: [Corrected] Style/ParenthesesAroundCondition: Don't use parentheses around the condition of an if.
if (foo; bar)
   ^^^^^^^^^^
foo.rb:9:8: C: [Corrected] Style/Semicolon: Do not use semicolons to terminate expressions.
if (foo; bar)
       ^
foo.rb:10:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 1) spaces for indentation.
 bar
^
foo.rb:11:2: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
 puts "condition is true"
 ^^^^^^^^^^^^^^^^^^^^^^^^
foo.rb:11:3: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
  puts "condition is true"
  ^^^^^^^^^^^^^^^^^^^^^^^^

Steps to reproduce the problem

  1. Create a file named foo.rb
  2. Copy and paste the code at the top of this issue into foo.rb
  3. Run rubocop foo.rb
  4. Run rubocop --auto-correct foo.rb

RuboCop version

1.22.3 (using Parser 3.0.2.0, rubocop-ast 1.12.0, running on ruby 2.7.4 x86_64-darwin20)

@koic koic added the bug label Nov 2, 2021
koic added a commit to koic/rubocop that referenced this issue Nov 2, 2021
…Condition`

Fixes rubocop#10227.

This PR fixes a false positive for `Style/ParenthesesAroundCondition` when
parentheses in multiple expressions separated by semicolon.
koic added a commit that referenced this issue Nov 2, 2021
…ntheses_around_condition

[Fix #10227] Fix a false positive 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