Sha256: adcb937381ac229fe51ebbb368383548b1261324d4006effa1e5b225ba12bb42

Contents?: true

Size: 1.32 KB

Versions: 17

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Do not compute the size of statically sized objects.
      class FixedSize < Cop
        MSG = 'Do not compute the size of statically sized objects.'.freeze

        def_node_matcher :counter, <<-MATCHER
          (send ${array hash str sym} {:count :length :size} $...)
        MATCHER

        def on_send(node)
          return if allowed_parent?(node.parent)

          counter(node) do |var, arg|
            return if allowed_variable?(var) || allowed_argument?(arg)

            add_offense(node, :expression)
          end
        end

        private

        def allowed_variable?(var)
          contains_splat?(var) || contains_double_splat?(var)
        end

        def allowed_argument?(arg)
          arg && non_string_argument?(arg.first)
        end

        def allowed_parent?(node)
          node && (node.casgn_type? || node.block_type?)
        end

        def contains_splat?(node)
          return unless node.array_type?

          node.each_child_node(:splat).any?
        end

        def contains_double_splat?(node)
          return unless node.hash_type?

          node.each_child_node(:kwsplat).any?
        end

        def non_string_argument?(node)
          node && !node.str_type?
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/fixed_size.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/fixed_size.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/fixed_size.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/fixed_size.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/fixed_size.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/fixed_size.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.49.1 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.49.0 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.48.1 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.48.0 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.47.1 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.47.0 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.46.0 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.45.0 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.44.1 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.44.0 lib/rubocop/cop/performance/fixed_size.rb