class RuboCop::Cop::Style::CollectionQuerying

Prefer ‘Enumerable` predicate methods over expressions with `count`.

The cop checks calls to ‘count` without arguments, or with a block. It doesn’t register offenses for ‘count` with a positional argument because its behavior differs from predicate methods (`count` matches the argument using `==`, while `any?`, `none?` and `one?` use `===`).

NOTE: This cop doesn’t check ‘length` and `size` methods because they would yield false positives. For example, `String` implements `length` and `size`, but it doesn’t include ‘Enumerable`.

@safety

The cop is unsafe because receiver might not include `Enumerable`, or
it has nonstandard implementation of `count` or any replacement
methods.

It's also unsafe because for collections with falsey values, expressions
with `count` without a block return a different result than methods `any?`,
`none?` and `one?`:

[source,ruby]
----
[nil, false].count.positive?
[nil].count == 1
# => true

[nil, false].any?
[nil].one?
# => false

[nil].count == 0
# => false

[nil].none?
# => true
----

Autocorrection is unsafe when replacement methods don't iterate over
every element in collection and the given block runs side effects:

[source,ruby]
----
x.count(&:method_with_side_effects).positive?
# calls `method_with_side_effects` on every element

x.any?(&:method_with_side_effects)
# calls `method_with_side_effects` until first element returns a truthy value
----

@example

# bad
x.count.positive?
x.count > 0
x.count != 0

x.count(&:foo?).positive?
x.count { |item| item.foo? }.positive?

# good
x.any?

x.any?(&:foo?)
x.any? { |item| item.foo? }

# bad
x.count.zero?
x.count == 0

# good
x.none?

# bad
x.count == 1
x.one?

@example AllCops:ActiveSupportExtensionsEnabled: false (default)

# good
x.count > 1

@example AllCops:ActiveSupportExtensionsEnabled: true

# bad
x.count > 1

# good
x.many?