class RuboCop::Cop::Lint::StructNewOverride
g.count #=> 2
g.clone #=> #<struct Good id=1, name=“foo”>
g.members #=> [:id, :name]
g = Good.new(1, “foo”)
Good = Struct.new(:id, :name)
# good
b.count #=> 1 (overriding ‘Enumerable#count`)
b.clone #=> true (overriding `Object#clone`)
b.members #=> [] (overriding `Struct#members`)
b = Bad.new([], true, 1)
Bad = Struct.new(:members, :clone, :count)
# bad
@example
via `Struct.new`.
Checks unexpected overrides of the `Struct` built-in methods
def on_send(node)
def on_send(node) return unless struct_new(node) do node.arguments.each_with_index do |arg, index| # Ignore if the first argument is a class name next if index.zero? && arg.str_type? # Ignore if the argument is not a member name next unless STRUCT_MEMBER_NAME_TYPES.include?(arg.type) member_name = arg.value next unless STRUCT_METHOD_NAMES.include?(member_name.to_sym) message = format(MSG, member_name: member_name.inspect, method_name: member_name.to_s) add_offense(arg, message: message) end end end