class RuboCop::Cop::Style::StructInheritance

This cop checks for inheritance from Struct.new.

@example

# bad
class Person < Struct.new(:first_name, :last_name)
end

# good
Person = Struct.new(:first_name, :last_name)

Constants

MSG

Public Instance Methods

on_class(node) click to toggle source
# File lib/rubocop/cop/style/struct_inheritance.rb, line 18
def on_class(node)
  _name, superclass, _body = *node
  return unless struct_constructor?(superclass)

  add_offense(node, superclass.source_range, MSG)
end

Private Instance Methods

struct_constructor?(node) click to toggle source
# File lib/rubocop/cop/style/struct_inheritance.rb, line 27
def struct_constructor?(node)
  return false unless node

  send_node = node.block_type? ? node.children.first : node
  return false unless send_node.send_type?

  receiver, method_name = *send_node

  receiver &&
    receiver.const_type? &&
    receiver.children.last == :Struct &&
    method_name == :new
end