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

AmbiguousBlockAssociation gives bad advice #4181

Closed
mvz opened this issue Mar 27, 2017 · 11 comments · May be fixed by juslee/java-buildpack#1
Closed

AmbiguousBlockAssociation gives bad advice #4181

mvz opened this issue Mar 27, 2017 · 11 comments · May be fixed by juslee/java-buildpack#1

Comments

@mvz
Copy link
Contributor

mvz commented Mar 27, 2017

The message printed for the AmbiguousBlockAssociation cop is confusing because it assumes an intent on the part of the developer that does not match the actual behavior of Ruby. I.e., the message only makes sense if the code in question is currently broken.

Here is an example:

foo bar { |val| baz val }

Here, the cop would print something like:

Parenthesize the param bar to make sure that the block will be associated with the foo method call.

However, the code as it stands associates the block with bar, and if the code is currently working, the programmer should definitely not parenthesize the parameter, because it would change the meaning of the code.

Expected behavior

I expect RuboCop to either make no suggestion on how to resolve the ambiguity, or to suggest something that does not change the semantics of the code as it stands.

Actual behavior

RuboCop suggests a change that always breaks the existing code.

Steps to reproduce the problem

  • Create a file foo.rb, containing:

    foo bar { |val| baz val }
    
  • Run rubocop foo.rb.

RuboCop version

0.48.0 (using Parser 2.4.0.0, running on ruby 2.4.0 x86_64-linux)
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 27, 2017

Well, the message of the cop could probably be better, but the advice is just what it's supposed to be. That's not a bug. Might not play well with some DSLs, but the cop works just as it's expected to. Such code is confusing and should ideally be avoided.

@bbatsov bbatsov closed this as completed Mar 27, 2017
@mvz
Copy link
Contributor Author

mvz commented Mar 27, 2017

Huh? The point of this bug report is that the message could be better. I'm not saying this should not be flagged as ambiguous.

@stephengroat
Copy link

we just started getting this error message which makes no sense at all:

verify.rb:59:8: W: Parenthesize the param sort_by to make sure that the block will be associated with the != method call.
   error('section.yml is not alphabetized by name') \
       if sections != sections.sort_by { |section| section['id'].downcase }
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

sort_by is not a param, it's a method, correct?

@mvz
Copy link
Contributor Author

mvz commented Mar 28, 2017

@stephengroat that looks like a different issue, so I think you should file a new ticket for it.

@stephengroat
Copy link

@mvz thanks will do

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 28, 2017

Huh? The point of this bug report is that the message could be better. I'm not saying this should not be flagged as ambiguous.

Suggestions for improvements are welcome. I'm not super fond of the current message myself.

@bbatsov bbatsov reopened this Mar 28, 2017
@smakagon
Copy link
Contributor

@mvz Feel free to suggest any improvements for description for this cop. I can change it and create PR when we all ok with that.

@ashmaroli
Copy link

ashmaroli commented Apr 2, 2017

Given: assert @site.pages.find { |page| page.data["date"] == date }
When I run rubocop against above,
I would like the warning message to have been:

W: Parenthesize the param @site.pages.find { |page| page.data["date"] == date } to make sure
that the block will be associated with the find
method call.
      assert @site.pages.find { |page| page.data["date"] == date }
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@smakagon
Copy link
Contributor

smakagon commented Apr 3, 2017

Hi guys, @mvz @ashmaroli @bbatsov
I've extended offense message of this cop in this PR to show more context for developer.

Now for this code:
Foo.some_method a { |el| puts el }

It will return this message:

Parenthesize the param a { |el| puts el } to make sure that the block will be associated with the a method call.

It should provide better guidance for developer on fixing this offense.
What do you think?

smakagon added a commit to smakagon/rubocop that referenced this issue Apr 3, 2017
@ashmaroli
Copy link

@smakagon Yes, that's more understandable and in line with what I suggested in the comment above. 👍

@mvz
Copy link
Contributor Author

mvz commented Apr 3, 2017

Yes, that's much better!

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 a pull request may close this issue.

5 participants