Sha256: eee492a1a18940f1287a2c49a110fbf058b75df3c1e4d0d473b7c9d64109e367

Contents?: true

Size: 1.8 KB

Versions: 15

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks unexpected overrides of the `Struct` built-in methods
      # via `Struct.new`.
      #
      # @example
      #   # bad
      #   Bad = Struct.new(:members, :clone, :count)
      #   b = Bad.new([], true, 1)
      #   b.members #=> [] (overriding `Struct#members`)
      #   b.clone #=> true (overriding `Object#clone`)
      #   b.count #=> 1 (overriding `Enumerable#count`)
      #
      #   # good
      #   Good = Struct.new(:id, :name)
      #   g = Good.new(1, "foo")
      #   g.members #=> [:id, :name]
      #   g.clone #=> #<struct Good id=1, name="foo">
      #   g.count #=> 2
      #
      class StructNewOverride < Cop
        MSG = '`%<member_name>s` member overrides `Struct#%<method_name>s`' \
              ' and it may be unexpected.'

        STRUCT_METHOD_NAMES = Struct.instance_methods
        STRUCT_MEMBER_NAME_TYPES = %i[sym str].freeze

        def_node_matcher :struct_new, <<~PATTERN
          (send
            (const ${nil? cbase} :Struct) :new ...)
        PATTERN

        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
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
rubocop-0.88.0 lib/rubocop/cop/lint/struct_new_override.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/lint/struct_new_override.rb
rubocop-0.87.1 lib/rubocop/cop/lint/struct_new_override.rb
rubocop-0.87.0 lib/rubocop/cop/lint/struct_new_override.rb
rubocop-0.86.0 lib/rubocop/cop/lint/struct_new_override.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/lint/struct_new_override.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/lint/struct_new_override.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/lint/struct_new_override.rb
rubocop-0.85.1 lib/rubocop/cop/lint/struct_new_override.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/lint/struct_new_override.rb
rubocop-0.85.0 lib/rubocop/cop/lint/struct_new_override.rb
rubocop-0.84.0 lib/rubocop/cop/lint/struct_new_override.rb
rubocop-0.83.0 lib/rubocop/cop/lint/struct_new_override.rb
rubocop-0.82.0 lib/rubocop/cop/lint/struct_new_override.rb
rubocop-0.81.0 lib/rubocop/cop/lint/struct_new_override.rb