Class: Hone::Patterns::HashMergeBang
- Defined in:
- lib/hone/patterns/hash_merge_bang.rb
Overview
Pattern: hash = hash.merge(other) -> hash.merge!(other)
Reassigning to the same variable after merge creates a new hash. merge! mutates in place, avoiding allocation.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
#add_finding, inherited, #initialize, scan_file
Constructor Details
This class inherits a constructor from Hone::Patterns::Base
Instance Method Details
#visit_local_variable_write_node(node) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hone/patterns/hash_merge_bang.rb', line 13 def visit_local_variable_write_node(node) super # Look for: x = x.merge(...) value = node.value return unless value.is_a?(Prism::CallNode) return unless value.name == :merge receiver = value.receiver return unless receiver.is_a?(Prism::LocalVariableReadNode) return unless receiver.name == node.name add_finding( node, message: "Use `.merge!` instead of `#{node.name} = #{node.name}.merge(...)` to avoid creating new hash", speedup: "Avoids creating new hash, mutates in place" ) end |