Skip to content

Commit

Permalink
Fix Lint/UselessMethodDefinition to not register an offense when me…
Browse files Browse the repository at this point in the history
…thod definition includes optional arguments
  • Loading branch information
fatkodima authored and marcandre committed Sep 7, 2020
1 parent 79b43f7 commit 521d7d6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
### Changes

* [#8470](https://github.com/rubocop-hq/rubocop/issues/8470): Do not autocorrect `Style/StringConcatenation` when parts of the expression are too complex. ([@dvandersluis][])
* [#8561](https://github.com/rubocop-hq/rubocop/issues/8561): Fix `Lint/UselessMethodDefinition` to not register an offense when method definition includes optional arguments. ([@fatkodima][])
* [#8617](https://github.com/rubocop-hq/rubocop/issues/8617): Fix `Style/HashAsLastArrayItem` to not register an offense when all items in an array are hashes. ([@dvandersluis][])

## 0.90.0 (2020-09-01)
Expand Down
5 changes: 5 additions & 0 deletions docs/modules/ROOT/pages/cops_lint.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4621,6 +4621,11 @@ def method
super
end
# good - with default arguments
def initialize(x = Object.new)
super
end
# good
def initialize
initialize_internals
Expand Down
10 changes: 10 additions & 0 deletions lib/rubocop/cop/lint/useless_method_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ module Lint
# super
# end
#
# # good - with default arguments
# def initialize(x = Object.new)
# super
# end
#
# # good
# def initialize
# initialize_internals
Expand Down Expand Up @@ -46,6 +51,7 @@ class UselessMethodDefinition < Base
MSG = 'Useless method definition detected.'

def on_def(node)
return if optional_args?(node)
return unless (constructor?(node) && empty_constructor?(node)) ||
delegating?(node.body, node)

Expand All @@ -55,6 +61,10 @@ def on_def(node)

private

def optional_args?(node)
node.arguments.any? { |arg| arg.optarg_type? || arg.kwoptarg_type? }
end

def empty_constructor?(node)
return false if node.body
return false if cop_config['AllowComments'] && comment_lines?(node)
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/lint/useless_method_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ def method2(foo, bar)
RUBY
end

it 'does not register an offense when method definition contains optional argument' do
expect_no_offenses(<<~RUBY)
def method(x = 1)
super
end
RUBY
end

it 'does not register an offense when method definition contains optional keyword argument' do
expect_no_offenses(<<~RUBY)
def method(x: 1)
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 521d7d6

Please sign in to comment.