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

Ignore UnusedBlockArgument for keyword arguments #2314

Merged
merged 1 commit into from
Mar 30, 2016

Conversation

volkert
Copy link

@volkert volkert commented Oct 9, 2015

This PR is related to #2304 with a fix for unused block arguments.

Rubocop currently suggests to prepend an underscore to unused keyword arguments for blocks. This would break the keyword argument and theres no other way around it but ignoring the fact that there are unused keyword arguments.

@volkert volkert force-pushed the unused_block_keyword_argument branch 2 times, most recently from 85d4b0f to 86ae301 Compare October 9, 2015 13:51
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 10, 2015

Apart from the failing build, the change looks reasonable to me.

context 'when a keyword argument is not used' do
let(:source) { <<-END }
1.times do |foo:|
puts 'foo'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Something like define_method is the only reasonable use of keyword arguments with blocks I can think of, so it would be a better example here.

@volkert volkert force-pushed the unused_block_keyword_argument branch 2 times, most recently from e1aeb97 to b83fbc2 Compare October 12, 2015 07:58
@volkert
Copy link
Author

volkert commented Oct 12, 2015

The build still fails for ruby 1.9.3 because it's unable to parse code with keyword arguments successfully. Any good ideas on how to work around that? I could return from the test early if RUBY_VERSION < '2.0.0'

@maxjacobson
Copy link
Contributor

@volkert volkert force-pushed the unused_block_keyword_argument branch from b83fbc2 to 627912c Compare October 13, 2015 08:15
@volkert
Copy link
Author

volkert commented Oct 13, 2015

@maxjacobson thanks! The build now passes for all versions.

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 15, 2015

Btw, is the real issue the reporting or the suggested correction? I think it's worth reporting those by default, unless someone opts it's not (meaning this should probably be a config option).

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 23, 2015

@volkert ping

@volkert volkert force-pushed the unused_block_keyword_argument branch 2 times, most recently from d83a571 to 6b3940d Compare November 3, 2015 13:45
expect(cop.highlights).to eq(['bar'])
expect(cop.offenses.first.message).to eq(
'Unused block argument - `bar`. ' \
"You can omit the argument if you don't care about it."
Copy link
Author

Choose a reason for hiding this comment

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

Sorry for the late response! I made it configurable through AllowUnusedKeywordArguments now. I'm not sure about the error message here, though. It' actually not true that the argument can be omitted. If it is omitted ruby will complain about an unknown keyword. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

If the user controls both the caller and callee, they can remove an unneeded keyword arg on both sides. If they only control the callee, then that is a problem.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, this message didn't really take keyword args into account. Maybe it should be different for them?

@volkert
Copy link
Author

volkert commented Nov 12, 2015

@bbatsov Anything left to improve here? I'd be happy to help bringing this one to master!

@bbatsov
Copy link
Collaborator

bbatsov commented Nov 13, 2015

Btw, is the real issue the reporting or the suggested correction? I think it's worth reporting those by default, unless someone opts it's not (meaning this should probably be a config option).

You never replied to this comment of mine.

@volkert
Copy link
Author

volkert commented Nov 13, 2015

Actually both. The suggested correction broke the code and I'm not sure how useful it is to report unused keywords, especially if the only way of fixing it is adding an option to ignore keywords. There is no good way of improving your code to prevent the violation. But I'd be fine with how it is right now.

@volkert
Copy link
Author

volkert commented Dec 10, 2015

@bbatsov so, how do we proceed with this?

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 10, 2015

I'm still not sure what exactly is this change supposed to fix. Some real examples would help.

@volkert
Copy link
Author

volkert commented Dec 10, 2015

I have a test which expects a method to be called with keyword arguments that I want to run an expect on:

expect(Foo).to receive(:bar) do |one_param:, another_object:|
  expect(another_object.bar).to eql(0)
end

(similar to my real code)

If I run rubocop on this, it complains that:

Unused block argument - one_param. If it's necessary, use _ or _one_param as an argument name to indicate that it won't be used.
    expect(Foo).to receive(:bar) do |one_param:, another_object:|

The problem with the renaming is that it is a keyword argument, so the name matters here. If I replace it with an underscore or prefix it, rubocop is happy but ruby is sad because of a missing keyword.

ArgumentError: missing keyword: _one_param

So it's either ruby or rubocop complaining, there is no way of making both happy.

My first approach for the PR to fix this was to not check for unused keywords at all because I cannot think of a situation where one is able to rename the keyword. But since you preferred having an opt-out for this, I changed my PR. This is similar to #2304.

Sorry for causing so much confustion. I hope I managed to explain my situation and we can bring this to master soon ;)
Let me know if I can help in any way!

@volkert volkert force-pushed the unused_block_keyword_argument branch from 6b3940d to 7698a4c Compare December 10, 2015 16:27
@volkert
Copy link
Author

volkert commented Jan 22, 2016

@bbatsov any updates on this?

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 3, 2016

Sorry for the late response - too much things to do... I see your point now. Rebase this and I'll accept it.

@volkert volkert force-pushed the unused_block_keyword_argument branch 6 times, most recently from b071412 to fc7aad3 Compare March 29, 2016 15:58
@volkert
Copy link
Author

volkert commented Mar 30, 2016

@bbatsov I rebased it and it should be fine to merge it now. I had to do some minor changes for the tests/rubocop to pass.

@@ -420,6 +420,7 @@
* [#2393](https://github.com/bbatsov/rubocop/pull/2393): `Style/MethodCallParentheses` doesn't fail on `obj.method ||= func()`. ([@alexdowad][])
* [#2344](https://github.com/bbatsov/rubocop/pull/2344): When autocorrecting, `Style/ParallelAssignment` reorders assignment statements, if necessary, to avoid breaking code. ([@alexdowad][])
* `Style/MultilineOperationAlignment` does not try to align the receiver and selector of a method call if both are on the LHS of an assignment. ([@alexdowad][])
* [#2314](https://github.com/bbatsov/rubocop/pull/2314): Ignore `UnusedBlockArgument` for keyword arguments. ([@volkert][])
Copy link
Collaborator

Choose a reason for hiding this comment

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

You'll have to move this to the master section.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 30, 2016

@volkert Just address my note about the outdated position of the changelog entry.

@volkert volkert force-pushed the unused_block_keyword_argument branch from fc7aad3 to 96125dd Compare March 30, 2016 11:43
@volkert
Copy link
Author

volkert commented Mar 30, 2016

Done!

@@ -1,6 +1,7 @@
# Change log

## master (unreleased)
* [#2314](https://github.com/bbatsov/rubocop/pull/2314): Ignore `UnusedBlockArgument` for keyword arguments. ([@volkert][])
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be 3 lines down under bug fixes I guess.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

@volkert volkert force-pushed the unused_block_keyword_argument branch from 96125dd to 8f01fbe Compare March 30, 2016 11:49
@bbatsov bbatsov merged commit 7e2ee32 into rubocop:master Mar 30, 2016
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 30, 2016

👍 Great success!

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.

4 participants