Class: Hone::Patterns::RangeInclude
- Defined in:
- lib/hone/patterns/range_include.rb
Overview
Pattern: range.include?(x) -> range.cover?(x)
include? iterates the range to check membership. cover? just checks if value is between bounds (O(1)).
Note: Semantics differ for non-numeric ranges. cover? checks bounds only, include? checks actual membership. For numeric ranges they're equivalent.
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_call_node(node) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/hone/patterns/range_include.rb', line 16 def visit_call_node(node) super # Look for: .include?(x) on a range literal return unless node.name == :include? return unless node.arguments&.arguments&.size == 1 receiver = node.receiver return unless receiver.is_a?(Prism::RangeNode) add_finding( node, message: "Use `.cover?` instead of `.include?` on ranges for O(1) bounds check", speedup: "O(n) iteration to O(1) comparison (for numeric ranges)" ) end |