Skip to content

Commit

Permalink
Lint/MissingSuper: exclude method_missing and respond_to_missing?
Browse files Browse the repository at this point in the history
Contrary to the other hooks, these can be completely taken over:

```ruby
def method_missing(method, *args)
  nil
end

def method_missing(method, *args)
  delegate_to.send(method, *args)
end
```

While a class has a "right" to expect its other hooks to be called, these hooks aren't necessarily called the same way.
  • Loading branch information
marcandre authored and bbatsov committed Nov 21, 2020
1 parent ba4e402 commit e7d30be
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9072](https://github.com/rubocop-hq/rubocop/pull/9072): `Lint/MissingSuper`: exclude `method_missing` and `respond_to_missing?`. ([@marcandre][])
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,7 @@ Lint/MissingSuper:
without calls to `super`'.
Enabled: true
VersionAdded: '0.89'
VersionChanged: '<<next>>'

Lint/MixedRegexpCaptureTypes:
Description: 'Do not mix named captures and numbered captures in a Regexp literal.'
Expand Down
11 changes: 7 additions & 4 deletions lib/rubocop/cop/lint/missing_super.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ module Lint
# This cop checks for the presence of constructors and lifecycle callbacks
# without calls to `super`.
#
# This cop does not consider `method_missing` (and `respond_to_missing?`)
# because in some cases it makes sense to overtake what is considered a
# missing method. In other cases, the theoretical ideal handling could be
# challenging or verbose for no actual gain.
#
# @example
# # bad
# class Employee < Person
Expand Down Expand Up @@ -43,15 +48,13 @@ class MissingSuper < Base

STATELESS_CLASSES = %w[BasicObject Object].freeze

OBJECT_LIFECYCLE_CALLBACKS = %i[method_missing respond_to_missing?].freeze
CLASS_LIFECYCLE_CALLBACKS = %i[inherited].freeze
METHOD_LIFECYCLE_CALLBACKS = %i[method_added method_removed method_undefined
singleton_method_added singleton_method_removed
singleton_method_undefined].freeze

CALLBACKS = (OBJECT_LIFECYCLE_CALLBACKS +
CLASS_LIFECYCLE_CALLBACKS +
METHOD_LIFECYCLE_CALLBACKS).to_set.freeze
CALLBACKS = (CLASS_LIFECYCLE_CALLBACKS +
METHOD_LIFECYCLE_CALLBACKS).to_set.freeze

def on_def(node)
return unless offender?(node)
Expand Down
3 changes: 2 additions & 1 deletion spec/project_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true
version_regexp = /\A\d+\.\d+\z|\A<<next>>\z/

RSpec.describe 'RuboCop Project', type: :feature do
let(:cop_names) do
Expand All @@ -11,6 +10,8 @@
.map(&:cop_name)
end

version_regexp = /\A\d+\.\d+\z|\A<<next>>\z/

describe 'default configuration file' do
subject(:config) { RuboCop::ConfigLoader.load_file('config/default.yml') }

Expand Down
32 changes: 1 addition & 31 deletions spec/rubocop/cop/lint/missing_super_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,41 +94,11 @@ def method_added(*)
RUBY
end

it 'registers an offense for instance level `method_missing?` with no `super` call' do
expect_offense(<<~RUBY)
class Foo
def method_missing(*args)
^^^^^^^^^^^^^^^^^^^^^^^^^ Call `super` to invoke callback defined in the parent class.
end
end
RUBY
end

it 'registers an offense for class level `method_missing?` with no `super` call' do
expect_offense(<<~RUBY)
class Foo
def self.method_missing(*args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Call `super` to invoke callback defined in the parent class.
end
end
RUBY
end

it 'does not register an offense when `method_missing?` contains `super` call' do
expect_no_offenses(<<~RUBY)
class Foo
def method_missing(*args)
super
do_something
end
end
RUBY
end

it 'does not register an offense when callback has a `super` call' do
expect_no_offenses(<<~RUBY)
class Foo
def self.inherited(base)
do_something
super
end
end
Expand Down

0 comments on commit e7d30be

Please sign in to comment.