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

Rails/Delegate ignores ruby 2.3 Safe Nagivation Operator #3040

Closed
PizzaPill opened this issue Apr 13, 2016 · 3 comments
Closed

Rails/Delegate ignores ruby 2.3 Safe Nagivation Operator #3040

PizzaPill opened this issue Apr 13, 2016 · 3 comments
Labels

Comments

@PizzaPill
Copy link

The following line of code:

object_that_can_be_nil&.foo gets converted to delegate :foo, to: :object_that_can_be_nil, prefix: true which is not equivalent. The equivalent would be delegate :foo, to: :object_that_can_be_nil, prefix: true, allow_nil: true.

This all happens in rubocop 0.39.0 and parameter auto-correct enabled.

@jonas054
Copy link
Collaborator

@geniou You created that cop. Can you look into this?

@andialles
Copy link

andialles commented Nov 2, 2016

Same thing happens here:

def foo
    bar.foo if bar
end

gets converted to delegate :foo, to: :bar without the allow_nil: true parameter

@jonas054 jonas054 added the bug label Nov 5, 2016
@cgriego
Copy link
Contributor

cgriego commented Jun 29, 2017

One gotcha here is that allow_nil is not equivalent to &. because allow_nil checks that the receiver is not nil or if it responds to the method. So methods that nil responds to are also delegated. This is problematic, but the only handy example I have is with whiny nils.

# nil if foo is nil
def foo_id
  foo&.id
end

# RuntimeError if foo is nil and whiny nils
delegate :id, to: :foo, prefix: true, allow_nil: true

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

No branches or pull requests

4 participants