Class: Hone::Patterns::PositivePredicate

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

Overview

Pattern: (expr).positive? -> (expr) > 0

In hot paths, the method call overhead of .positive? can add up. Direct comparison is faster.

From sqids-ruby commit 8a74142

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



15
16
17
18
19
20
21
22
23
24
# File 'lib/hone/patterns/positive_predicate.rb', line 15

def visit_call_node(node)
  super
  return unless node.name == :positive? && node.arguments.nil?

  add_finding(
    node,
    message: "Use `> 0` instead of `.positive?` in hot paths to avoid method call overhead",
    speedup: "Minor, but adds up in tight loops"
  )
end