Sha256: 994d43afb276fc53ca4f451f3b82f93776fd677d975ea241df52d1d7244e63ea

Contents?: true

Size: 834 Bytes

Versions: 9

Compression:

Stored size: 834 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for duplicated keys in hash literals.
      # This cop considers both primitive types and constants for the hash keys.
      #
      # This cop mirrors a warning in Ruby 2.2.
      #
      # @example
      #
      #   # bad
      #   hash = { food: 'apple', food: 'orange' }
      #
      #   # good
      #   hash = { food: 'apple', other_food: 'orange' }
      class DuplicateHashKey < Base
        include Duplication

        MSG = 'Duplicated key in hash literal.'

        def on_hash(node)
          keys = node.keys.select { |key| key.recursive_basic_literal? || key.const_type? }

          return unless duplicates?(keys)

          consecutive_duplicates(keys).each { |key| add_offense(key) }
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.69.2 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.69.1 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.69.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.68.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.67.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.66.1 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.66.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.65.1 lib/rubocop/cop/lint/duplicate_hash_key.rb