class RuboCop::Cop::Style::ZeroLengthPredicate

!hash.empty?
!string.empty?
!{a: 1, b: 2}.empty?
array.empty?
“foobar”.empty?
[1, 2, 3].empty?
# good
hash.size > 0
string.length > 0
{a: 1, b: 2}.length != 0
array.length < 1
0 == “foobar”.length
[1, 2, 3].length == 0
# bad
@example
may register a false positive.
is a non-standard class that redefines ‘length` or `empty?`, the cop
has an `empty?` method that is defined in terms of `length`. If there
This cop is unsafe because it cannot be guaranteed that the receiver
@safety
so allow `size == 0` and `size.zero?`.
NOTE: `File`, `Tempfile`, and `StringIO` do not have `empty?`
replaced by `receiver.empty?` and `!receiver.empty?`.
`receiver.length < 1` and `receiver.size == 0` that can be
`receiver.length > 0`, and `receiver.length != 0`,
by a predicate method, such as `receiver.length == 0`,
Checks for numeric comparisons that can be replaced

def check_nonzero_length_comparison(node)

def check_nonzero_length_comparison(node)
  nonzero_length_comparison = nonzero_length_comparison(node.parent)
  return unless nonzero_length_comparison
  lhs, opr, rhs = nonzero_length_comparison
  return if non_polymorphic_collection?(node.parent)
  add_offense(
    node.parent, message: format(NONZERO_MSG, current: "#{lhs} #{opr} #{rhs}")
  ) do |corrector|
    corrector.replace(node.parent, replacement(node.parent))
  end
end

def check_zero_length_comparison(node)

def check_zero_length_comparison(node)
  zero_length_comparison = zero_length_comparison(node.parent)
  return unless zero_length_comparison
  lhs, opr, rhs = zero_length_comparison
  return if non_polymorphic_collection?(node.parent)
  add_offense(
    node.parent, message: format(ZERO_MSG, current: "#{lhs} #{opr} #{rhs}")
  ) do |corrector|
    corrector.replace(node.parent, replacement(node.parent))
  end
end

def check_zero_length_predicate(node)

def check_zero_length_predicate(node)
  return unless (length_method = zero_length_predicate(node.parent))
  return if non_polymorphic_collection?(node.parent)
  offense = node.loc.selector.join(node.parent.source_range.end)
  message = format(ZERO_MSG, current: "#{length_method}.zero?")
  add_offense(offense, message: message) do |corrector|
    corrector.replace(offense, 'empty?')
  end
end

def on_send(node)

def on_send(node)
  check_zero_length_predicate(node)
  check_zero_length_comparison(node)
  check_nonzero_length_comparison(node)
end

def replacement(node)

def replacement(node)
  receiver = zero_length_receiver(node)
  return "#{receiver.source}.empty?" if receiver
  "!#{other_receiver(node).source}.empty?"
end