hash-compare-by-identity
What it does
Section titled “What it does”Reports a hash looked up with x.object_id as the key (h[x.object_id], key?, fetch).
Why it matters
Section titled “Why it matters”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.
Example
Section titled “Example”...foo (a ? b : c)foo(1)h[x.object_id]h.key?(x.id)...2.times { f }...Configuration
Section titled “Configuration”ruby: lint: rules: hash-compare-by-identity: warn # off (default), warn, errorEnabled 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.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| rubocop | Lint/HashCompareByIdentity |
full |