class RuboCop::Cop::Lint::SafeNavigationWithEmpty
return unless foo && foo.empty?
return if foo && foo.empty?
# good
return unless foo&.empty?
return if foo&.empty?
# bad
@example
do the opposite of what the author intends.
checking ‘foo&.empty?` in a conditional, `foo` being `nil` will actually
While the safe navigation operator is generally a good idea, when
a conditional.
This cop checks to make sure safe navigation isn’t used with ‘empty?` in
def on_if(node)
def on_if(node) return unless safe_navigation_empty_in_conditional?(node) add_offense(node) end