Parent

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Style::EmptyLiteral

This cop checks for the use of a method, the result of which would be a literal, like an empty array, hash or string.

Constants

ARRAY_NODE

Empty array node

(send

(const nil :Array) :new)
ARR_MSG
HASH_MSG
HASH_NODE

Empty hash node

(send

(const nil :Hash) :new)
STR_MSG
STR_NODE

Empty string node

(send

(const nil :String) :new)

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 46
def autocorrect(node)
  @corrections << lambda do |corrector|
    name = case node
           when ARRAY_NODE then '[]'
           when HASH_NODE then '{}'
           when STR_NODE then "''"
           end
    corrector.replace(node.loc.expression, name)
  end
end
on_send(node) click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 30
def on_send(node)
  return if part_of_ignored_node?(node)

  case node
  when ARRAY_NODE
    add_offense(node, :expression, ARR_MSG)
  when HASH_NODE
    add_offense(node, :expression, HASH_MSG)
  when STR_NODE
    add_offense(node, :expression, STR_MSG)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.