Skip to content

RuboCop::Cop::Performance::Count is not safe and should be moved to the unsafe set #8

@jon-rse

Description

@jon-rse

Expected behavior: Running standardrb --fix will not break code.
Observed behavior: standard --fix includes cops which are unsafe.
Environment: Ruby 3.2.0, default standardrb install,

standard (1.28.2)
      language_server-protocol (~> 3.17.0.2)
      lint_roller (~> 1.0)
      rubocop (~> 1.50.2)
      standard-custom (~> 1.0.0)
      standard-performance (~> 1.0.1)

The count cop will transform

 hash.select { |k,v| k == "count me" }.count

into

 hash.count { |k,v| k == "count me" }

However the count method on a hash does not pass |k,v| to the block, but rather an array, [k,v]. This is not a safe transformation, and requires modification to the block, or the caller for the transformed code to operate correctly eg:

 hash.keys.count { |k| k == "count me" }

See this issue in rubocop where they mark the count cop as unsafe, rubocop/rubocop-performance#69

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions