Sha256: 617ae005041f6eeaa005cdbe567feb3e0d964b691c9fefe4b759f8dca637fc08

Contents?: true

Size: 1.77 KB

Versions: 24

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop check for uses of Object#freeze on immutable objects.
      #
      # @example
      #   # bad
      #   CONST = 1.freeze
      #
      #   # good
      #   CONST = 1
      class RedundantFreeze < Base
        extend AutoCorrector
        include FrozenStringLiteral

        MSG = 'Do not freeze immutable objects, as freezing them has no ' \
              'effect.'
        RESTRICT_ON_SEND = %i[freeze].freeze

        def on_send(node)
          return unless node.receiver &&
                        (immutable_literal?(node.receiver) ||
                         operation_produces_immutable_object?(node.receiver))

          add_offense(node) do |corrector|
            corrector.remove(node.loc.dot)
            corrector.remove(node.loc.selector)
          end
        end

        private

        def immutable_literal?(node)
          node = strip_parenthesis(node)

          return true if node.immutable_literal?

          FROZEN_STRING_LITERAL_TYPES.include?(node.type) &&
            frozen_string_literals_enabled?
        end

        def strip_parenthesis(node)
          if node.begin_type? && node.children.first
            node.children.first
          else
            node
          end
        end

        def_node_matcher :operation_produces_immutable_object?, <<~PATTERN
          {
            (begin (send {float int} {:+ :- :* :** :/ :% :<<} _))
            (begin (send !(str _) {:+ :- :* :** :/ :%} {float int}))
            (begin (send _ {:== :=== :!= :<= :>= :< :>} _))
            (send (const {nil? cbase} :ENV) :[] _)
            (send _ {:count :length :size} ...)
            (block (send _ {:count :length :size} ...) ...)
          }
        PATTERN
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_freeze.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_freeze.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_freeze.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_freeze.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_freeze.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.6.1 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.6.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.5.2 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.5.1 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.5.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.4.2 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.4.1 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.4.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.3.1 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.3.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.2.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.1.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-1.0.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-0.93.1 lib/rubocop/cop/style/redundant_freeze.rb