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 Lint/UselessMethodDefinition to not register an offense when method definition includes optional arguments #8659

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 @@ -19,6 +19,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
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
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
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