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

Flag offence for empty parentheses in single line method definitions #10421

Closed
samrjenkins opened this issue Feb 16, 2022 · 1 comment · Fixed by #10425
Closed

Flag offence for empty parentheses in single line method definitions #10421

samrjenkins opened this issue Feb 16, 2022 · 1 comment · Fixed by #10425
Labels

Comments

@samrjenkins
Copy link

Problem

Rubocop allows single line method definitions to use empty parentheses.
Eg.

def method_1() 'foo'; end
def method_2() = 'bar'

Though this behaviour has been part of Rubocop for many years, as far as I can see it is contrary to the style guide which expressly instructs against using parentheses in a method definition when there are no parameters:

Method Definition Parentheses

Use def with parentheses when there are parameters. Omit the parentheses when the method doesn’t accept any parameters.

Link: Method Definition Parentheses

I have been unable to find an exception to this rule for single line methods anywhere in the guide (though I might be missing it).

Solution

Style/DefWithParentheses flags offences for single line method definitions with parentheses which do not accept parameters

Alternatives

I have been unable to discover the rationale behind originally allowing empty parentheses in this commit. If it is behaviour we want to retain, perhaps there should be a mention of it in the style guide (assuming there isn't one already which I have been unable to find).

@koic
Copy link
Member

koic commented Feb 17, 2022

def foo() do_something end will continue to allow because def foo do_something end is a syntax error.
On the other hand, be aware that the endless method definition's parentheses can be omitted. I've opened #10421.

koic added a commit to koic/rubocop that referenced this issue Feb 20, 2022
…method definition

Fixes rubocop#10421.

This PR makes `Style/DefWithParentheses` aware of endless method definition.

`def foo() do_something end` will continue to allow because `def foo do_something end`
is a syntax error.
On the other hand, be aware that the endless method definition's parentheses can be omitted.
bbatsov pushed a commit that referenced this issue Feb 20, 2022
…definition

Fixes #10421.

This PR makes `Style/DefWithParentheses` aware of endless method definition.

`def foo() do_something end` will continue to allow because `def foo do_something end`
is a syntax error.
On the other hand, be aware that the endless method definition's parentheses can be omitted.
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