Skip to content

hash-compare-by-identity

Rubysuspiciousno fixdefault: offruby/rubocop

Reports a hash looked up with x.object_id as the key (h[x.object_id], key?, fetch).

The hash then emulates identity with an integer, which also keeps no reference to the object and may collide after the object is freed. compare_by_identity does it by design.

To fix, call h.compare_by_identity and use the object as the key.

Reported
...
foo (a ? b : c)
foo(1)
h[x.object_id]
h.key?(x.id)
...
2.times { f }
...
ruby:
lint:
rules:
hash-compare-by-identity: warn # off (default), warn, error

Enabled by the presets: ruby/rubocop.

Suppress one line with sudsy-disable-next-line hash-compare-by-identity in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
rubocop Lint/HashCompareByIdentity full