Module: RuboCop::Cop::NegativeConditional

Included in:
Style::NegatedIf, Style::NegatedWhile
Defined in:
lib/rubocop/cop/mixin/negative_conditional.rb

Overview

Some common code shared between FavorUnlessOverNegatedIf and FavorUntilOverNegatedWhile.

Instance Method Summary collapse

Instance Method Details

#check_negative_conditional(node) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rubocop/cop/mixin/negative_conditional.rb', line 8

def check_negative_conditional(node)
  condition, _body, _rest = *node

  # Look at last expression of contents if there's a parenthesis
  # around condition.
  condition = condition.children.last while condition.type == :begin
  return unless condition.type == :send

  _object, method = *condition
  return unless method == :! && !(node.loc.respond_to?(:else) &&
                                  node.loc.else)

  add_offense(node, :expression)
end