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 a false positive for Style/TrailingBodyOnMethodDefinition #9548

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#9548](https://github.com/rubocop/rubocop/pull/9548): Fix a false positive for `Style/TrailingBodyOnMethodDefinition` when endless method definition body is after newline in opening parenthesis. ([@koic][])
Expand Up @@ -34,6 +34,7 @@ class TrailingBodyOnMethodDefinition < Base

def on_def(node)
return unless trailing_body?(node)
return if node.endless? && node.body.parenthesized_call?

add_offense(first_part_of(node.body)) do |corrector|
LineBreakCorrector.correct_trailing_body(
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/style/trailing_body_on_method_definition_spec.rb
Expand Up @@ -91,6 +91,16 @@ def some_method
RUBY
end

context 'Ruby 3.0 or higher', :ruby30 do
it 'does not register offense when endless method definition body is after newline in opening parenthesis' do
expect_no_offenses(<<~RUBY)
def some_method = (
body
)
RUBY
end
end

it 'auto-corrects with comment after body' do
expect_offense(<<-RUBY.strip_margin('|'))
| def some_method; body # stuff
Expand Down