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

Style/SafeNavigation-20230303233259 #837

Merged
merged 3 commits into from Mar 6, 2023

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Mar 3, 2023

Rubocop challenge!

Style/SafeNavigation

Safe autocorrect: No
⚠️ The autocorrect a cop can yield false positives by design.

Description

Overview

Transforms usages of a method call safeguarded by a non nil
check for the variable whose method is being called to
safe navigation (&.). If there is a method chain, all of the methods
in the chain need to be checked for safety, and all of the methods will
need to be changed to use safe navigation.

The default for ConvertCodeThatCanStartToReturnNil is false.
When configured to true, this will
check for code in the format !foo.nil? && foo.bar. As it is written,
the return of this code is limited to false and whatever the return
of the method is. If this is converted to safe navigation,
foo&.bar can start returning nil as well as what the method
returns.

The default for MaxChainLength is 2
We have limited the cop to not register an offense for method chains
that exceed this option is set.

Examples

# bad
foo.bar if foo
foo.bar.baz if foo
foo.bar(param1, param2) if foo
foo.bar { |e| e.something } if foo
foo.bar(param) { |e| e.something } if foo

foo.bar if !foo.nil?
foo.bar unless !foo
foo.bar unless foo.nil?

foo && foo.bar
foo && foo.bar.baz
foo && foo.bar(param1, param2)
foo && foo.bar { |e| e.something }
foo && foo.bar(param) { |e| e.something }

foo ? foo.bar : nil
foo.nil? ? nil : foo.bar
!foo.nil? ? foo.bar : nil
!foo ? nil : foo.bar

# good
foo&.bar
foo&.bar&.baz
foo&.bar(param1, param2)
foo&.bar { |e| e.something }
foo&.bar(param) { |e| e.something }
foo && foo.bar.baz.qux # method chain with more than 2 methods
foo && foo.nil? # method that `nil` responds to

# Method calls that do not use `.`
foo && foo < bar
foo < bar if foo

# When checking `foo&.empty?` in a conditional, `foo` being `nil` will actually
# do the opposite of what the author intends.
foo && foo.empty?

# This could start returning `nil` as well as the return of the method
foo.nil? || foo.bar
!foo || foo.bar

# Methods that are used on assignment, arithmetic operation or
# comparison should not be converted to use safe navigation
foo.baz = bar if foo
foo.baz + bar if foo
foo.bar > 2 if foo

Auto generated by rubocop_challenger

@mathieujobin mathieujobin reopened this Mar 6, 2023
@mathieujobin mathieujobin merged commit f28011e into master Mar 6, 2023
@mathieujobin mathieujobin deleted the rubocop-challenge/20230303233259 branch March 6, 2023 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant