Sha256: a3942516c8422921064d212ddb5ee96a62a7084b48f6c289476f4a2af37af841

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

# encoding: utf-8
# 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)
          counter(node) do |variable, arg|
            return if contains_splat?(variable)
            return if contains_double_splat?(variable)
            return if !arg.nil? && string_argument?(arg.first)
            if node.parent
              return if node.parent.casgn_type? || node.parent.block_type?
            end

            add_offense(node, :expression)
          end
        end

        private

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

          node.children.any? do |child|
            child.respond_to?(:splat_type?) && child.splat_type?
          end
        end

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

          node.children.any? do |child|
            child.respond_to?(:kwsplat_type?) && child.kwsplat_type?
          end
        end

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

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/performance/fixed_size.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.42.0 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.41.2 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.41.1 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.41.0 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.40.0 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.39.0 lib/rubocop/cop/performance/fixed_size.rb
rubocop-0.38.0 lib/rubocop/cop/performance/fixed_size.rb