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 new Lint/SafeNavigationChain cop #3804

Merged
merged 1 commit into from
Dec 20, 2016

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Dec 20, 2016

Safe navigation operator returns nil if the receiver is nil.
So if you chain an ordinary method call after safe navigation operator, it raises NoMethodError.
We should use safe navigation operator after safe navigation operator.
This cop checks the problem.

For example

x = nil
x&.foo.bar   # => NoMethodError: undefined method `bar' for nil:NilClass
x&.foo&.bar  # => nil

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

Safe navigation operator returns nil if the receiver is nil.
So if you chain an ordinary method call after safe navigation operator, it raises NoMethodError.
We should use safe navigation operator after safe navigation operator.
This cop checks the problem.

For example

```ruby
x = nil
x&.foo.bar   # => NoMethodError: undefined method `bar' for nil:NilClass
x&.foo&.bar  # => nil
```
@pocke pocke force-pushed the cop-for-safe-navigation-chain branch from eac22bc to c9a3d08 Compare December 20, 2016 07:19
@bbatsov bbatsov merged commit af9b24a into rubocop:master Dec 20, 2016
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 20, 2016

Another great contribution! Thanks! 👍 ❤️ 🚀

@pocke pocke deleted the cop-for-safe-navigation-chain branch December 20, 2016 08:16

def autocorrect(node)
dot = node.loc.dot
return unless node.loc.dot
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's a bit late now, but I guess we could have used the local variable dot here. 😀

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll fix this myself. :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🙀 🙀 Thanks! 🙇

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