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 ReturnInVoidContext cop when return is not inside a method. #4737

Merged
merged 1 commit into from
Sep 24, 2017
Merged

Fix ReturnInVoidContext cop when return is not inside a method. #4737

merged 1 commit into from
Sep 24, 2017

Conversation

frodsan
Copy link

@frodsan frodsan commented Sep 15, 2017

Fixes #4736

@@ -45,7 +45,7 @@ def on_return(return_node)

def method_name(return_node)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am inclined to split this into two methods, one that finds the context, and one that extracts the name from a def node.

Right now these two are mixed, and causes this method to have a "special meaning nil" mixed return type.

I would rather see something like:

context_node = non_void_context(return_node)

return unless context_node && ...

add_offense(return_node, :keyword, format(message, method_name(context_node))

CHANGELOG.md Outdated
@@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#4737](https://github.com/bbatsov/rubocop/pull/4737): Fix ReturnInVoidContext cop when `return` is not inside a method. ([@frodsan][])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically "not inside a context", or "in top scope". 🙂

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use return in any scope, so the description seems reasonable to me, unless I missing something @Drenmi.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm wrong - I tested this and you can use it only in method/lambda/proc or in the top scope. My bad.

@@ -80,4 +80,12 @@ def self.initialize
RUBY
end
end

context 'with no method' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, too: "when return is in top scope".

Copy link
Collaborator

@bbatsov bbatsov Sep 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess we can also have some tests with procs and lambdas. Guess we didn't think of all of them in the past.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

Copy link
Collaborator

@Drenmi Drenmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🌮

Please have a look at the tiny comments I made. 🙂

@frodsan
Copy link
Author

frodsan commented Sep 21, 2017

@Drenmi updated

end

def useless_return_method?(method_name)
method_name == :initialize || method_setter?(method_name)
end

def method_setter?(method_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#method_setter? -> #setter_method? 🙂

method_name.to_s.end_with?('=') &&
!AST::Node::COMPARISON_OPERATORS.include?(method_name)
def method_name(context_node)
context_node.children.first
end

def useless_return_method?(method_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be #void_context_method?

Copy link
Collaborator

@Drenmi Drenmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small naming suggestions, but ready to approve this. 🙂

@frodsan
Copy link
Author

frodsan commented Sep 21, 2017

@Drenmi updated

@bbatsov bbatsov merged commit 0f35c5f into rubocop:master Sep 24, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 24, 2017

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants