Sha256: 082098057badd4087b675b0268ed832fbf68dfe5c881fa03ba0cbbce04d12313

Contents?: true

Size: 1.25 KB

Versions: 12

Compression:

Stored size: 1.25 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 = 'Do not freeze immutable objects, as freezing them has no ' \
              'effect.'.freeze

        def on_send(node)
          return unless node.receiver && node.method?(:freeze) &&
                        immutable_literal?(node.receiver)

          add_offense(node)
        end

        def autocorrect(node)
          lambda 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
      end
    end
  end
end

Version data entries

12 entries across 10 versions & 2 rubygems

Version Path
rubocop-0.65.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-0.64.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-0.63.1 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-0.63.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-0.62.0 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-0.61.1 lib/rubocop/cop/style/redundant_freeze.rb
rubocop-0.61.0 lib/rubocop/cop/style/redundant_freeze.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/redundant_freeze.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/redundant_freeze.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/redundant_freeze.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/redundant_freeze.rb
rubocop-0.60.0 lib/rubocop/cop/style/redundant_freeze.rb