Class: Hone::Patterns::StringShovel

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

Overview

Pattern: str = str + other -> str << other

When reassigning a local variable to itself plus another string, using the shovel operator (<<) avoids creating a new string object. The + operator always creates a new string, while << mutates in place.

Example:

# Before - creates new string
str = str + other
str = str + "suffix"

# After - mutates in place
str << other
str << "suffix"

Note: Only safe when the original string can be mutated (not frozen, not shared with other references that expect the original value).

Impact: Avoids allocating a new string object

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



28
29
30
31
32
# File 'lib/hone/patterns/string_shovel.rb', line 28

def visit_local_variable_write_node(node)
  super

  check_string_reassignment(node, node.name)
end