Class: RuboCop::Cop::Performance::FixedSize

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/performance/fixed_size.rb

Overview

Do not compute the size of statically sized objects.

Examples:

# String methods
# bad
'foo'.size
%q[bar].count
%(qux).length

# Symbol methods
# bad
:fred.size
:'baz'.length

# Array methods
# bad
[1, 2, thud].count
%W(1, 2, bar).size

# Hash methods
# bad
{ a: corge, b: grault }.length

# good
foo.size
bar.count
qux.length

# good
:"#{fred}".size
CONST = :baz.length

# good
[1, 2, *thud].count
garply = [1, 2, 3]
garply.size

# good
{ a: corge, **grault }.length
waldo = { a: corge, b: grault }
waldo.size

Constant Summary collapse

MSG =
'Do not compute the size of statically sized objects.'
RESTRICT_ON_SEND =
%i[count length size].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/cop/performance/fixed_size.rb', line 56

def on_send(node)
  return if node.ancestors.any? { |ancestor| allowed_parent?(ancestor) }

  counter(node) do |var, arg|
    return if allowed_variable?(var) || allowed_argument?(arg)

    add_offense(node)
  end
end