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 #8330] Fix a false positive for Style/MissingRespondToMissing #8421

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,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