Module: RuboCop::Cop::NegativeConditional

Extended by:
NodePattern::Macros
Includes:
IfNode
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

Methods included from NodePattern::Macros

def_node_matcher, def_node_search, node_search, node_search_all, node_search_body, node_search_first

Methods included from IfNode

#elsif?, #if_else?, #if_node_parts, #modifier_if?, #ternary?

Instance Method Details

#check_negative_conditional(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rubocop/cop/mixin/negative_conditional.rb', line 14

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

  # Look at last expression of contents if there are parentheses
  # around condition.
  condition = condition.children.last while condition.begin_type?

  return unless single_negative?(condition) && !if_else?(node)

  add_offense(node, :expression)
end