Skip to content

Commit

Permalink
[Fix #8330] Fix a false positive for Style/MissingRespondToMissing
Browse files Browse the repository at this point in the history
Fixes #8330.

This PR fixes a false positive for `Style/MissingRespondToMissing`
when defined method with inline access modifier.
  • Loading branch information
koic authored and bbatsov committed Jul 31, 2020
1 parent c126da4 commit 02daa3d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,7 @@
* [#8385](https://github.com/rubocop-hq/rubocop/pull/8385): Remove auto-correction for `Lint/EnsureReturn`. ([@marcandre][])
* [#8391](https://github.com/rubocop-hq/rubocop/issues/8391): Mark `Style/ArrayCoercion` as not safe. ([@marcandre][])
* [#8406](https://github.com/rubocop-hq/rubocop/issues/8406): Improve `Style/AccessorGrouping`'s auto-correction to remove redundant blank lines. ([@koic][])
* [#8330](https://github.com/rubocop-hq/rubocop/issues/8330): Fix a false positive for `Style/MissingRespondToMissing` when defined method with inline access modifier. ([@koic][])

### Changes

Expand Down
11 changes: 9 additions & 2 deletions lib/rubocop/cop/style/missing_respond_to_missing.rb
Expand Up @@ -36,9 +36,16 @@ def on_def(node)
private

def implements_respond_to_missing?(node)
node.parent.each_child_node(node.type).any? do |sibling|
sibling.method?(:respond_to_missing?)
return false unless (grand_parent = node.parent.parent)

grand_parent.each_descendant(node.type) do |descendant|
return true if descendant.method?(:respond_to_missing?)

child = descendant.children.first
return true if child.respond_to?(:method?) && child.method?(:respond_to_missing?)
end

false
end
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cop/style/missing_respond_to_missing_spec.rb
Expand Up @@ -52,6 +52,31 @@ def self.method_missing
RUBY
end

it 'allows method_missing and respond_to_missing? when defined with inline access modifier' do
expect_no_offenses(<<~RUBY)
class Test
private def respond_to_missing?
end
private def method_missing
end
end
RUBY
end

it 'allows method_missing and respond_to_missing? when defined with inline access modifier and ' \
'method_missing is not qualified by inline access modifier' do
expect_no_offenses(<<~RUBY)
class Test
private def respond_to_missing?
end
def method_missing
end
end
RUBY
end

it 'registers an offense respond_to_missing? is implemented as ' \
'an instance method and method_missing is implemented as a class method' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 02daa3d

Please sign in to comment.