Class: Hone::Patterns::StringEndWith
- Defined in:
- lib/hone/patterns/string_end_with.rb
Overview
Pattern: str == 'x' or str.match?(/x$/) -> str.end_with?('x')
Indexing at position -1 for comparison or using regex with $ anchor is less clear and potentially slower than using end_with?.
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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/hone/patterns/string_end_with.rb', line 21 def visit_call_node(node) super if index_negative_one_comparison?(node) add_finding( node, message: "Use `end_with?` instead of `str[-1] == ...` for cleaner code", speedup: "Cleaner and avoids substring/regex overhead" ) elsif regex_end_anchor?(node) add_finding( node, message: "Use `end_with?` instead of `match?(/...$/)` to avoid regex overhead", speedup: "Cleaner and avoids substring/regex overhead" ) end end |