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 an incorrect auto-correct for Style/SingleLineMethods #9909

Conversation

koic
Copy link
Member

@koic koic commented Jul 3, 2021

This PR fixes an incorrect auto-correct for Style/SingleLineMethods when using return, break, or next for one line method body in Ruby 3.0.

These are not supported in the endless method definition.

% ruby -v
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-darwin19]

% cat example.rb
def foo(argument) break bar(argument); end
def foo(argument) next bar(argument); end
def foo(argument) return bar(argument); end

% ruby -c example.rb
Syntax OK

% bundle exec rubocop --only Style/SingleLineMethods -a
(snip)

Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/SingleLineMethods: Avoid
single-line method definitions.
def foo(argument) break bar(argument); end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:2:1: C: [Corrected] Style/SingleLineMethods: Avoid
single-line method definitions.
def foo(argument) next bar(argument); end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:3:1: C: [Corrected] Style/SingleLineMethods: Avoid
single-line method definitions.
def foo(argument) return bar(argument); end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 3 offenses detected, 3 offenses corrected

% cat example.rb
def foo(argument) = break bar(argument)
def foo(argument) = next bar(argument)
def foo(argument) = return bar(argument)

% ruby -ce 'def foo(argument) = break bar(argument)'
-e:1: syntax error, unexpected local variable or method, expecting
end-of-input
def foo(argument) = break bar(argument)

% ruby -ce 'def foo(argument) = next bar(argument)'
-e:1: syntax error, unexpected local variable or method, expecting
end-of-input
def foo(argument) = next bar(argument)

% ruby -ce 'def foo(argument) = return bar(argument)'
-e:1: syntax error, unexpected local variable or method, expecting
end-of-input
def foo(argument) = return bar(argument)

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

This PR fixes an incorrect auto-correct for `Style/SingleLineMethods`
when using `return`, `break`, or `next` for one line method body in Ruby 3.0.

These are not supported in the endless method definition.

```console
% ruby -v
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-darwin19]

% cat example.rb
def foo(argument) break bar(argument); end
def foo(argument) next bar(argument); end
def foo(argument) return bar(argument); end

% ruby -c example.rb
Syntax OK

% bundle exec rubocop --only Style/SingleLineMethods -a
(snip)

Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/SingleLineMethods: Avoid
single-line method definitions.
def foo(argument) break bar(argument); end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:2:1: C: [Corrected] Style/SingleLineMethods: Avoid
single-line method definitions.
def foo(argument) next bar(argument); end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:3:1: C: [Corrected] Style/SingleLineMethods: Avoid
single-line method definitions.
def foo(argument) return bar(argument); end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 3 offenses detected, 3 offenses corrected

% cat example.rb
def foo(argument) = break bar(argument)
def foo(argument) = next bar(argument)
def foo(argument) = return bar(argument)

% ruby -ce 'def foo(argument) = break bar(argument)'
-e:1: syntax error, unexpected local variable or method, expecting
end-of-input
def foo(argument) = break bar(argument)

% ruby -ce 'def foo(argument) = next bar(argument)'
-e:1: syntax error, unexpected local variable or method, expecting
end-of-input
def foo(argument) = next bar(argument)

% ruby -ce 'def foo(argument) = return bar(argument)'
-e:1: syntax error, unexpected local variable or method, expecting
end-of-input
def foo(argument) = return bar(argument)
```
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 6, 2021

Interesting edge case! How did you even find this?

@bbatsov bbatsov merged commit ccdf562 into rubocop:master Jul 6, 2021
@koic koic deleted the fix_incorrect_autocorrect_for_style_single_line_methods branch July 6, 2021 07:45
@koic
Copy link
Member Author

koic commented Jul 6, 2021

I found it while investigating standardrb/standard#309. There may be several other problems with this issue, so I'm looking at them in order.

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.

None yet

2 participants