Skip to content

Commit

Permalink
Optimize duplicate hash key warning (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgollahon authored Apr 15, 2023
1 parent f1da317 commit 6b1c42b
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,34 +538,23 @@ def kwsplat(dstar_t, arg)
end

def associate(begin_t, pairs, end_t)
0.upto(pairs.length - 1) do |i|
(i + 1).upto(pairs.length - 1) do |j|
pair_i = pairs[i]
pair_j = pairs[j]
key_set = Set.new

next if pair_i.type != :pair || pair_j.type != :pair
pairs.each do |pair|
next unless pair.type.eql?(:pair)

key1, = *pair_i
key2, = *pair_j
key, = *pair

do_warn = false

# keys have to be simple nodes, MRI ignores equal composite keys like
# `{ a(1) => 1, a(1) => 1 }`
case key1.type
when :sym, :str, :int, :float
if key1 == key2
do_warn = true
end
when :rational, :complex, :regexp
if @parser.version >= 31 && key1 == key2
do_warn = true
end
end
case key.type
when :sym, :str, :int, :float
when :rational, :complex, :regexp
next unless @parser.version >= 31
else
next
end

if do_warn
diagnostic :warning, :duplicate_hash_key, nil, key2.loc.expression
end
unless key_set.add?(key)
diagnostic :warning, :duplicate_hash_key, nil, key.loc.expression
end
end

Expand Down

0 comments on commit 6b1c42b

Please sign in to comment.