Class: Hone::Patterns::StringEmpty

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

Overview

Pattern: str.length == 0 or str.size == 0 -> str.empty?

Comparing length/size to 0 is less idiomatic than using empty?. empty? is the Ruby way to check for emptiness.

Examples:

Bad

str.length == 0
str.size == 0
0 == str.length
0 == str.size

Good

str.empty?

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



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hone/patterns/string_empty.rb', line 22

def visit_call_node(node)
  super

  return unless length_or_size_equals_zero?(node)

  add_finding(
    node,
    message: "Use `empty?` instead of comparing `length`/`size` to 0",
    speedup: "Minor, but more idiomatic"
  )
end