Skip to content

Commit

Permalink
Fix an incorrect auto-correct for Style/RedundantParentheses
Browse files Browse the repository at this point in the history
This PR fixes an incorrect auto-correct for `Style/RedundantParentheses`
when enclosed in parentheses at `while-post` or `until-post`.

The following is a reproduction step.

```console
% rubocop example1.rb --only Style/RedundantParentheses -a
Inspecting 1 file
E

Offenses:

example1.rb:3:5: E: Lint/Syntax: unexpected token tIDENTIFIER
(Using Ruby 2.2 parser; configure using TargetRubyVersion parameter,
under AllCops)
end whilefoo
    ^^^^^^^^
example1.rb:3:10: C: [Corrected] Style/RedundantParentheses:
Don't use parentheses around a method call.
end while(foo)
    ^^^^^

1 file inspected, 2 offenses detected, 1 offense corrected
```

This is a broken `while`.

```diff
% git diff
diff --git a/example1.rb b/example1.rb
index cf5523f..5f47ae4 100644
--- a/example1.rb
+++ b/example1.rb
@@ -1,3 +1,3 @@
 begin
   do_something
-end while(foo)
+end whilefoo
```

This will not be warned when `while` ... `end`.

```consle
% cat example2.rb
while(foo)
  do_something
end

% rubocop example2.rb --only Style/RedundantParentheses
Inspecting 1 file
.

1 file inspected, no offenses detected
```

So this PR changes to the same behavior.
  • Loading branch information
koic authored and bbatsov committed Apr 29, 2019
1 parent 9790976 commit b0262a5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#6995](https://github.com/rubocop-hq/rubocop/pull/6995): Fix an incorrect auto-correct for `Style/RedundantParentheses` when enclosed in parentheses at `while-post` or `until-post`. ([@koic][])

## 0.68.0 (2019-04-29)

### New features
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/redundant_parentheses.rb
Expand Up @@ -28,6 +28,8 @@ class RedundantParentheses < Cop

def on_begin(node)
return if !parentheses?(node) || parens_allowed?(node)
return if node.parent && (node.parent.while_post_type? ||
node.parent.until_post_type?)

check(node)
end
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Expand Up @@ -197,6 +197,22 @@ def x
RUBY
end

it 'accepts parentheses when enclosed in parentheses at `while-post`' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
do_something
end while(bar)
RUBY
end

it 'accepts parentheses when enclosed in parentheses at `until-post`' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
do_something
end until(bar)
RUBY
end

it 'accepts parentheses when they touch the preceding keyword' do
expect_no_offenses('if x; y else(1) end')
end
Expand Down

0 comments on commit b0262a5

Please sign in to comment.