Class: Hone::Patterns::HashValuesInclude

Inherits:
Base
  • Object
show all
Defined in:
lib/hone/patterns/hash_values_include.rb

Overview

Pattern: hash.values.include?(x) -> hash.value?(x)

values.include? creates an intermediate array of values then searches. value? checks directly without allocation.

Instance Attribute Summary

Attributes inherited from Base

#findings

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_call_node(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hone/patterns/hash_values_include.rb', line 13

def visit_call_node(node)
  super

  # Look for: .include?(arg) where receiver is .values
  return unless node.name == :include? && node.arguments&.arguments&.size == 1

  receiver = node.receiver
  return unless receiver.is_a?(Prism::CallNode)
  return unless receiver.name == :values

  add_finding(
    node,
    message: "Use `.value?(x)` instead of `.values.include?(x)` to avoid intermediate array",
    speedup: "Avoids creating intermediate array of values"
  )
end