Sha256: 16e6197b606cd46eca7890b3713d9e1f5e798afe3f2ffa13b729e462c0a00f6a

Contents?: true

Size: 1.39 KB

Versions: 98

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Checks for `OpenStruct.new` calls.
      # Instantiation of an `OpenStruct` invalidates
      # Ruby global method cache as it causes dynamic method
      # definition during program runtime.
      # This could have an effect on performance,
      # especially in case of single-threaded
      # applications with multiple `OpenStruct` instantiations.
      #
      # @safety
      #   This cop is unsafe because `OpenStruct.new` and `Struct.new`
      #   are not equivalent.
      #
      # @example
      #   # bad
      #   class MyClass
      #     def my_method
      #       OpenStruct.new(my_key1: 'my_value1', my_key2: 'my_value2')
      #     end
      #   end
      #
      #   # good
      #   class MyClass
      #     MyStruct = Struct.new(:my_key1, :my_key2)
      #     def my_method
      #       MyStruct.new('my_value1', 'my_value2')
      #     end
      #   end
      #
      class OpenStruct < Base
        MSG = 'Consider using `Struct` over `OpenStruct` to optimize the performance.'
        RESTRICT_ON_SEND = %i[new].freeze

        def_node_matcher :open_struct, <<~PATTERN
          (send (const {nil? cbase} :OpenStruct) :new ...)
        PATTERN

        def on_send(node)
          open_struct(node) do
            add_offense(node.loc.selector)
          end
        end
      end
    end
  end
end

Version data entries

98 entries across 98 versions & 7 rubygems

Version Path
rubocop-performance-1.24.0 lib/rubocop/cop/performance/open_struct.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-performance-1.23.1/lib/rubocop/cop/performance/open_struct.rb
rubocop-performance-1.23.1 lib/rubocop/cop/performance/open_struct.rb
rubocop-performance-1.23.0 lib/rubocop/cop/performance/open_struct.rb
rubocop-performance-1.22.1 lib/rubocop/cop/performance/open_struct.rb
rubocop-performance-1.22.0 lib/rubocop/cop/performance/open_struct.rb
rubocop-performance-1.21.1 lib/rubocop/cop/performance/open_struct.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.21.0/lib/rubocop/cop/performance/open_struct.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/open_struct.rb
rubocop-performance-1.21.0 lib/rubocop/cop/performance/open_struct.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.20.2/lib/rubocop/cop/performance/open_struct.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/open_struct.rb
rubocop-performance-1.20.2 lib/rubocop/cop/performance/open_struct.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/open_struct.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/open_struct.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/open_struct.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/open_struct.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/open_struct.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/open_struct.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/open_struct.rb