Skip to content

RuboCop Gitlab/KeysFirstAndValuesFirst: Add missing specs and improve implementation

What does this MR do and why?

This MR add missing specs for 👮 Gitlab/KeysFirstAndValuesFirst and improves its implementation (support for constants, literal hashes, and local variables).

Contributes to #355012 and refs !96880 (merged).

Example offenses

foo.rb:6:15: C: Gitlab/KeysFirstAndValuesFirst: Prefer .each_key.first over .keys.first. This reduces memory usage and execution time.
    hash.keys.first
              ^^^^^
foo.rb:7:17: C: Gitlab/KeysFirstAndValuesFirst: Prefer .each_value.first over .values.first. This reduces memory usage and execution time.
    hash.values.first
                ^^^^^
foo.rb:8:16: C: Gitlab/KeysFirstAndValuesFirst: Prefer .each_key.first over .keys.first. This reduces memory usage and execution time.
    CONST.keys.first
               ^^^^^
foo.rb:9:18: C: Gitlab/KeysFirstAndValuesFirst: Prefer .each_value.first over .values.first. This reduces memory usage and execution time.
    CONST.values.first
                 ^^^^^

How to set up and validate locally

bin/rspec spec/rubocop/cop/gitlab/keys_first_and_values_first_spec.rb

bundle exec rubocop --only Gitlab/KeysFirstAndValuesFirst --parallel --format simple
27879 files inspected, no offenses detected

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Peter Leitzen

Merge request reports

Loading