class RuboCop::Cop::Style::DoubleNegation

end
!!return_value
define_singleton_method :foo? do
end
!!return_value
define_method :foo? do
end
!!return_value
def foo?
# bad
@example EnforcedStyle: forbidden
end
!!return_value
define_singleton_method :foo? do
end
!!return_value
define_method :foo? do
end
!!return_value
def foo?
# good
@example EnforcedStyle: allowed_in_returns (default)
!something.nil?
# good
!!something
# bad
@example
—-
!false.nil? #=> true
!!false #=> false
—-
[source,ruby]

of the expression will change.
Autocorrection is unsafe when the value is ‘false`, because the result
@safety
this is rarely a problem in practice.
As you’re unlikely to write code that can accept values of any type
`!!something` and ‘!something.nil?` are not the same thing.
NOTE: when `something` is a boolean value
should be forbidden always.
that use boolean as a return value. When using `EnforcedStyle: forbidden`, double negation
When using `EnforcedStyle: allowed_in_returns`, allow double negation in contexts
Checks for uses of double negation (`!!`) to convert something to a boolean value.

def allowed_in_returns?(node)

def allowed_in_returns?(node)
  node.parent&.return_type? || end_of_method_definition?(node)
end

def define_method?(node)

def define_method?(node)
  return false unless node.block_type?
  child = node.child_nodes.first
  return false unless child.send_type?
  child.method?(:define_method) || child.method?(:define_singleton_method)
end

def double_negative_condition_return_value?(node, last_child, conditional_node)

def double_negative_condition_return_value?(node, last_child, conditional_node)
  parent = find_parent_not_enumerable(node)
  if parent.begin_type?
    node.loc.line == parent.loc.last_line
  else
    last_child.last_line <= conditional_node.last_line
  end
end

def end_of_method_definition?(node)

def end_of_method_definition?(node)
  return false unless (def_node = find_def_node_from_ascendant(node))
  conditional_node = find_conditional_node_from_ascendant(node)
  last_child = find_last_child(def_node.send_type? ? def_node : def_node.body)
  if conditional_node
    double_negative_condition_return_value?(node, last_child, conditional_node)
  elsif last_child.pair_type? || last_child.hash_type? || last_child.parent.array_type?
    false
  else
    last_child.last_line <= node.last_line
  end
end

def find_conditional_node_from_ascendant(node)

def find_conditional_node_from_ascendant(node)
  return unless (parent = node.parent)
  return parent if parent.conditional?
  find_conditional_node_from_ascendant(parent)
end

def find_def_node_from_ascendant(node)

def find_def_node_from_ascendant(node)
  return unless (parent = node.parent)
  return parent if parent.def_type? || parent.defs_type?
  return node.parent.child_nodes.first if define_method?(parent)
  find_def_node_from_ascendant(node.parent)
end

def find_last_child(node)

def find_last_child(node)
  case node.type
  when :rescue
    find_last_child(node.body)
  when :ensure
    find_last_child(node.child_nodes.first)
  else
    node.child_nodes.last
  end
end

def find_parent_not_enumerable(node)

def find_parent_not_enumerable(node)
  return unless (parent = node.parent)
  if parent.pair_type? || parent.hash_type? || parent.array_type?
    find_parent_not_enumerable(parent)
  else
    parent
  end
end

def on_send(node)

def on_send(node)
  return unless double_negative?(node) && node.prefix_bang?
  return if style == :allowed_in_returns && allowed_in_returns?(node)
  location = node.loc.selector
  add_offense(location) do |corrector|
    corrector.remove(location)
    corrector.insert_after(node, '.nil?')
  end
end