Sha256: a47f36887d812dab8ad778a5ccb2a5ff40506558f3d5dadeb0a6b00a0105c504

Contents?: true

Size: 764 Bytes

Versions: 52

Compression:

Stored size: 764 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for duplicated keys in hash literals.
      #
      # This cop mirrors a warning in Ruby 2.2.
      #
      # @example
      #
      #   # bad
      #
      #   hash = { food: 'apple', food: 'orange' }
      #
      # @example
      #
      #   # 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(&:recursive_basic_literal?)

          return unless duplicates?(keys)

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

Version data entries

52 entries across 46 versions & 7 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/duplicate_hash_key.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/duplicate_hash_key.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/duplicate_hash_key.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/duplicate_hash_key.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.52.1 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.52.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.51.0 lib/rubocop/cop/lint/duplicate_hash_key.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/rubocop-1.50.2/lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.50.2 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.50.1 lib/rubocop/cop/lint/duplicate_hash_key.rb
rubocop-1.50.0 lib/rubocop/cop/lint/duplicate_hash_key.rb