Skip to content

Commit

Permalink
[Fix #11704] Fix a false positive for Lint/UselessMethodDefinition
Browse files Browse the repository at this point in the history
Fixes #11704.

This PR fixes a false positive for `Lint/UselessMethodDefinition`
when method definition with non access modifier containing only `super` call.
  • Loading branch information
koic authored and bbatsov committed Mar 18, 2023
1 parent 09e71c2 commit 32f4d0c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11704](https://github.com/rubocop/rubocop/issues/11704): Fix a false positive for `Lint/UselessMethodDefinition` when method definition with non access modifier containing only `super` call. ([@koic][])
12 changes: 10 additions & 2 deletions lib/rubocop/cop/lint/useless_method_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@ class UselessMethodDefinition < Base
MSG = 'Useless method definition detected.'

def on_def(node)
return if use_rest_or_optional_args?(node)
return if method_definition_with_modifier?(node) || use_rest_or_optional_args?(node)
return unless delegating?(node.body, node)

add_offense(node) { |corrector| corrector.remove(node) }
add_offense(node) do |corrector|
range = node.parent&.send_type? ? node.parent : node

corrector.remove(range)
end
end
alias on_defs on_def

private

def method_definition_with_modifier?(node)
node.parent&.send_type? && !node.parent&.non_bare_access_modifier?
end

def use_rest_or_optional_args?(node)
node.arguments.any? { |arg| arg.restarg_type? || arg.optarg_type? || arg.kwoptarg_type? }
end
Expand Down
52 changes: 52 additions & 0 deletions spec/rubocop/cop/lint/useless_method_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,50 @@ def self.other_useful_class_method
RUBY
end

it 'registers an offense and corrects when method definition with `public` access modifier containing only `super` call' do
expect_offense(<<~RUBY)
public def method
^^^^^^^^^^ Useless method definition detected.
super
end
RUBY

expect_correction("\n")
end

it 'registers an offense and corrects when method definition with `protected` access modifier containing only `super` call' do
expect_offense(<<~RUBY)
protected def method
^^^^^^^^^^ Useless method definition detected.
super
end
RUBY

expect_correction("\n")
end

it 'registers an offense and corrects when method definition with `private` access modifier containing only `super` call' do
expect_offense(<<~RUBY)
private def method
^^^^^^^^^^ Useless method definition detected.
super
end
RUBY

expect_correction("\n")
end

it 'registers an offense and corrects when method definition with `module_function` access modifier containing only `super` call' do
expect_offense(<<~RUBY)
module_function def method
^^^^^^^^^^ Useless method definition detected.
super
end
RUBY

expect_correction("\n")
end

it 'does not register an offense for method containing additional code to `super`' do
expect_no_offenses(<<~RUBY)
def method
Expand Down Expand Up @@ -170,6 +214,14 @@ def method(x: 1)
RUBY
end

it 'does not register an offense when method definition with generic method macro containing only `super` call' do
expect_no_offenses(<<~RUBY)
do_something def method
super
end
RUBY
end

it 'does not register an offense when non-constructor contains only comments' do
expect_no_offenses(<<~RUBY)
def non_constructor
Expand Down

0 comments on commit 32f4d0c

Please sign in to comment.