Rubocop: Lint/AmbiguousBlockAssociation cites unambiguous lambda param

Created on 27 Mar 2017  路  6Comments  路  Source: rubocop-hq/rubocop

When passing a lambda declaration as a method parameter, the new Lint/AmbiguousBlockAssociation cop forces unnecessary parentheses.

Example:

# cited
foo ->(a) { a }

# making the cop happy
foo(->(a) { a })

Note that the method does not generally require parentheses:

# good
foo a

Expected behavior

I expect Lint/AmbiguousBlockAssociation to not cite lambdas passed as params like any other object.

Actual behavior

The new Lint/AmbiguousBlockAssociation cop creates a citation for lambdas passed as params like any other object.

Steps to reproduce the problem

Create file:

foo ->(a) { a }

Run rubocop:

rubocop -D

RuboCop version

$ rubocop -V
0.48.0 (using Parser 2.4.0.0, running on ruby 2.3.3 x86_64-darwin16)

Most helpful comment

I got plain Rails scope like scope :technical, -> { where(technical: true) }
and I have

Parenthesize the param -> { where(technical: true) } to make sure that the block will be associated with the -> method call.

scope(:technical, -> { where(technical: true) }) fixes the problem but is it the way it should be?

scope :technical, (-> { where(technical: true) }) is good too but doesn't it look pretty strange?

All 6 comments

@Drenmi thanks for fixing that! I missed that case...

I got plain Rails scope like scope :technical, -> { where(technical: true) }
and I have

Parenthesize the param -> { where(technical: true) } to make sure that the block will be associated with the -> method call.

scope(:technical, -> { where(technical: true) }) fixes the problem but is it the way it should be?

scope :technical, (-> { where(technical: true) }) is good too but doesn't it look pretty strange?

@ivanovaleksey Are you using 0.48.1?

@bbatsov, yes

$ bundle exec rubocop -V
0.48.1 (using Parser 2.4.0.0, running on ruby 2.3.3 x86_64-darwin16)

Yeah, I'm seeing the same thing, on 0.48.1 for something like:

scope :vat, -> { where(post_subtotal_type: 'vat') }

@brandonweiss @bbatsov yes, there is a problem with lambda check. It's easily fixable. Should I create another PR to this issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndreiMotinga picture AndreiMotinga  路  3Comments

herwinw picture herwinw  路  3Comments

bbatsov picture bbatsov  路  3Comments

ecbrodie picture ecbrodie  路  3Comments

mikegee picture mikegee  路  3Comments