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

Add AllowUnusedKeywordArguments option for UnusedMethodArgument #2304

Merged
merged 1 commit into from
Oct 11, 2015

Conversation

apiology
Copy link
Contributor

@apiology apiology commented Oct 6, 2015

Hi!

The UnusedMethodArgument cop is great for finding unused method arguments. One reason why people might have unused method arguments is when they are subclassing or adhering to a duck type. In those cases, RuboCop suggests adding an underscore to the method argument.

This is normally great advice for positional arguments, but in Ruby, underscores in front of keyword arguments result in run-time errors when you try to pass in those keyword arguments--the underscore doesn't have the same semantics as positional arguments.

The net result is that if you have a project with a lot of subclassing and/or duck-types and keyword arguments, UnusedMethodArgument gives you a lot of false positives.

This change provides an option called "AllowUnusedKeywordArguments" to address this by disabling the cop for unused keyword arguments.

@jonas054
Copy link
Collaborator

jonas054 commented Oct 8, 2015

You're right, but since prepending an underscore to keyword arguments can change the semantics of calling the method, I think you should take the change even further.

Either AllowUnusedKeywordArguments should be true by default, or remove the option and never auto-correct keyword arguments.

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 8, 2015

Btw, this is also related to simply checking whether the body of a method is empty. Guess we can have an option to suppress this check for empty method bodies (as we can presume they're just abstract).

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 8, 2015

remove the option and never auto-correct keyword arguments.

Probably this would be best.

@apiology
Copy link
Contributor Author

apiology commented Oct 8, 2015

Keyword arguments are already ignored in auto-correct (see https://github.com/bbatsov/rubocop/blob/master/spec/rubocop/cop/lint/unused_method_argument_spec.rb#L310-L320), which is good news.

That said, I feel that this is not enough, since if you have a project with a lot of subclassing and/or duck-types and keyword arguments, UnusedMethodArgument gives you a lot of false positives.

I'd be happy to switch the default for AllowUnusedKeywordArguments over to false or leave it as true--thoughts?

@jonas054
Copy link
Collaborator

jonas054 commented Oct 9, 2015

Ah. Then I guess you can leave it as false (you said the other way around, but it is false now), since there's already basic support for the differentiation between positional and keyword arguments. Your addition is something extra for people who prefer peace and quiet and really don't want any reports about unused keyword arguments.

@apiology
Copy link
Contributor Author

apiology commented Oct 9, 2015

Yes, you're right--because in many cases those reports aren't actionable.

Ready to merge, then?

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 10, 2015

I'm OK with merging it, but the PR has to be rebased first.

@apiology apiology force-pushed the unused_keyword_method_argument branch from b29422b to 83978dc Compare October 11, 2015 00:19
@apiology
Copy link
Contributor Author

Sounds good--done!

bbatsov added a commit that referenced this pull request Oct 11, 2015
Add AllowUnusedKeywordArguments option for UnusedMethodArgument
@bbatsov bbatsov merged commit aa7d739 into rubocop:master Oct 11, 2015
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