Skip to content

Commit

Permalink
Fix a false positive for Style/TrailingBodyOnMethodDefinition
Browse files Browse the repository at this point in the history
This PR fixes a false positive for `Style/TrailingBodyOnMethodDefinition`
when endless method definition body is after newline in opening parenthesis.
  • Loading branch information
koic authored and bbatsov committed Feb 28, 2021
1 parent 6fb5d75 commit 8a4ee25
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
@@ -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

0 comments on commit 8a4ee25

Please sign in to comment.