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

Fix false positive in RedundantParentheses cop with multiple expression #3853

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Jan 3, 2017

Style/RedundantParentheses cop has a false positive.

For example.

 # test.rb
x ||= (
  foo
  bar
)
$ rubocop --only Style/RedundantParentheses test.rb
Inspecting 1 file
C

Offenses:

test.rb:2:7: C: Don't use parentheses around a method call.
x ||= ( ...
      ^

1 file inspected, 1 offense detected

However, the parentheses can't be omitted.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

@pocke pocke force-pushed the fix-redudant_parentheses-cop-with-multiple-expression branch from 8c09cb9 to ebe9368 Compare January 3, 2017 11:13
@@ -52,6 +52,8 @@
it_behaves_like 'redundant', '(true)', 'true', 'a literal'
it_behaves_like 'redundant', '(false)', 'false', 'a literal'
it_behaves_like 'redundant', '(/regexp/)', '/regexp/', 'a literal'
it_behaves_like 'plausible', '("x"; "y")'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those examples good? The parentheses look redundant to me here, as those could be two independent expressions that happen to be wrapped in parens...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean - don't you need an assignment operator preceding those expressions to be sure the parens are needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need an assignment operator preceding those expressions to be sure the parens are needed?

Yes, however, I need to consider other cases as well.

x = (
  foo
  bar
)


x + (
  foo
  bar
)


x((
  foo
  bar
))

If the parens is removed, the above code changes its meaning(and syntax error).
However, RuboCop adds offences to the code currently.

I'll update the cop. Thank you for your comment!

@pocke pocke force-pushed the fix-redudant_parentheses-cop-with-multiple-expression branch from ebe9368 to 9379a0c Compare January 6, 2017 10:43
…sion

Style/RedundantParentheses cop has a false positive.

For example.

```ruby
 # test.rb
x ||= (
  foo
  bar
)
```

```sh
$ rubocop --only Style/RedundantParentheses test.rb
Inspecting 1 file
C

Offenses:

test.rb:2:7: C: Don't use parentheses around a method call.
x ||= ( ...
      ^

1 file inspected, 1 offense detected
```

However, the parentheses can't be omitted.
@pocke pocke force-pushed the fix-redudant_parentheses-cop-with-multiple-expression branch from 9379a0c to e42c151 Compare January 6, 2017 11:03
@pocke
Copy link
Collaborator Author

pocke commented Jan 6, 2017

I fixed the behavior.

When parens's evaluated value is used, parens can not be omitted. As an exception, when the value is used as method/block's last value,

The implementation is referred to Lint/Void cop.

For example

# can't omit
x = (
  foo
  bar
)

# can omit
def x
  (
    foo
    bar
  )
end

# can omit
x do
  (
    foo
    bar
  )
end

@bbatsov bbatsov merged commit 27737c2 into rubocop:master Jan 6, 2017
@pocke pocke deleted the fix-redudant_parentheses-cop-with-multiple-expression branch January 7, 2017 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants