Sha256: c72b5ea516a23807b2a52a1f6891b0221062d9c516f5533000a5a068ff1b2ff1

Contents?: true

Size: 852 Bytes

Versions: 4

Compression:

Stored size: 852 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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)
      class StructInheritance < Cop
        MSG = "Don't extend an instance initialized by `Struct.new`.".freeze

        def on_class(node)
          _name, superclass, _body = *node
          return unless struct_constructor?(superclass)

          add_offense(node, superclass.source_range, MSG)
        end

        def_node_matcher :struct_constructor?, <<-PATTERN
           {(send (const nil :Struct) :new ...)
            (block (send (const nil :Struct) :new ...) ...)}
        PATTERN
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.49.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.49.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.48.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.48.0 lib/rubocop/cop/style/struct_inheritance.rb