Class: RuboCop::Cop::Homebrew::SafeNavigationWithBlank

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
rubocops/safe_navigation_with_blank.rb

Overview

Note:

While the safe navigation operator is generally a good idea, when checking foo&.blank? in a conditional, foo being nil will actually do the opposite of what the author intends.

For example:

[source,ruby]

foo&.blank? #=> nil foo.blank? #=> true —-

Checks to make sure safe navigation isn’t used with blank? in a conditional.

Examples:

# bad
do_something if foo&.blank?
do_something unless foo&.blank?

# good
do_something if foo.blank?
do_something unless foo.blank?

Constant Summary collapse

MSG =
"Avoid calling `blank?` with the safe navigation operator in conditionals."

Instance Method Summary collapse

Instance Method Details

#on_if(node) ⇒ Object



41
42
43
44
45
46
47
# File 'rubocops/safe_navigation_with_blank.rb', line 41

def on_if(node)
  return unless safe_navigation_blank_in_conditional?(node)

  add_offense(node) do |corrector|
    corrector.replace(safe_navigation_blank_in_conditional?(node).location.dot, ".")
  end
end