Sha256: 4e216c3ec403568b1ca4a55142f8b19bfc8396cadb573e0625b625286c8a4986

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 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 < Cop
        include FrozenStringLiteral

        MSG = 'Freezing immutable objects is pointless.'.freeze

        def on_send(node)
          receiver, method_name, *args = *node

          return unless method_name == :freeze &&
                        args.empty? &&
                        immutable_literal?(receiver)

          add_offense(node, :expression)
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.remove(node.loc.dot)
            corrector.remove(node.loc.selector)
          end
        end

        private

        def immutable_literal?(node)
          return false unless node
          return true if node.immutable_literal?
          FROZEN_STRING_LITERAL_TYPES.include?(node.type) &&
            frozen_string_literals_enabled?(processed_source)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.45.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-0.44.1 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-0.44.0 lib/rubocop/cop/style/redundant_freeze.rb