class RuboCop::Cop::Style::MutableConstant
Checks whether some constant value isn’t a mutable literal (e.g. array or hash).
Strict mode can be used to freeze all constants, rather than just literals. Strict mode is considered an experimental feature. It has not been updated with an exhaustive list of all methods that will produce frozen objects so there is a decent chance of getting some false positives. Luckily, there is no harm in freezing an already frozen object.
From Ruby 3.0, this cop honours the magic comment ‘shareable_constant_value’. When this magic comment is set to any acceptable value other than none, it will suppress the offenses raised by this cop. It enforces frozen state.
NOTE: ‘Regexp` and `Range` literals are frozen objects since Ruby 3.0.
NOTE: From Ruby 3.0, interpolated strings are not frozen when ‘# frozen-string-literal: true` is used, so this cop enforces explicit freezing for such strings.
NOTE: From Ruby 3.0, this cop allows explicit freezing of constants when the ‘shareable_constant_value` directive is used.
@safety
This cop's autocorrection is unsafe since any mutations on objects that are made frozen will change from being accepted to raising `FrozenError`, and will need to be manually refactored.
@example EnforcedStyle: literals (default)
# bad CONST = [1, 2, 3] # good CONST = [1, 2, 3].freeze # good CONST = <<~TESTING.freeze This is a heredoc TESTING # good CONST = Something.new
@example EnforcedStyle: strict
# bad CONST = Something.new # bad CONST = Struct.new do def foo puts 1 end end # good CONST = Something.new.freeze # good CONST = Struct.new do def foo puts 1 end end.freeze
@example
# Magic comment - shareable_constant_value: literal # bad CONST = [1, 2, 3] # good # shareable_constant_value: literal CONST = [1, 2, 3]
Constants
- MSG
Public Instance Methods
Source
# File lib/rubocop/cop/style/mutable_constant.rb, line 127 def on_casgn(node) if node.expression.nil? # This is only the case for `CONST += ...` or similarg66 parent = node.parent return unless parent.or_asgn_type? # We only care about `CONST ||= ...` on_assignment(parent.children.last) else on_assignment(node.expression) end end
Private Instance Methods
Source
# File lib/rubocop/cop/style/mutable_constant.rb, line 168 def autocorrect(corrector, node) expr = node.source_range splat_value = splat_value(node) if splat_value correct_splat_expansion(corrector, expr, splat_value) elsif node.array_type? && !node.bracketed? corrector.wrap(expr, '[', ']') elsif requires_parentheses?(node) corrector.wrap(expr, '(', ')') end corrector.insert_after(expr, '.freeze') end
Source
# File lib/rubocop/cop/style/mutable_constant.rb, line 157 def check(value) range_enclosed_in_parentheses = range_enclosed_in_parentheses?(value) return unless mutable_literal?(value) || (target_ruby_version <= 2.7 && range_enclosed_in_parentheses) return if frozen_string_literal?(value) return if shareable_constant_value?(value) add_offense(value) { |corrector| autocorrect(corrector, value) } end
Source
# File lib/rubocop/cop/style/mutable_constant.rb, line 207 def correct_splat_expansion(corrector, expr, splat_value) if range_enclosed_in_parentheses?(splat_value) corrector.replace(expr, "#{splat_value.source}.to_a") else corrector.replace(expr, "(#{splat_value.source}).to_a") end end
Source
# File lib/rubocop/cop/style/mutable_constant.rb, line 199 def frozen_regexp_or_range_literals?(node) target_ruby_version >= 3.0 && node.type?(:regexp, :range) end
Source
# File lib/rubocop/cop/style/mutable_constant.rb, line 189 def immutable_literal?(node) frozen_regexp_or_range_literals?(node) || node.immutable_literal? end
Source
# File lib/rubocop/cop/style/mutable_constant.rb, line 183 def mutable_literal?(value) return false if frozen_regexp_or_range_literals?(value) value.mutable_literal? end
Source
# File lib/rubocop/cop/style/mutable_constant.rb, line 140 def on_assignment(value) if style == :strict strict_check(value) else check(value) end end
Source
# File lib/rubocop/cop/style/mutable_constant.rb, line 203 def requires_parentheses?(node) node.range_type? || (node.send_type? && node.loc.dot.nil?) end
Source
# File lib/rubocop/cop/style/mutable_constant.rb, line 148 def strict_check(value) return if immutable_literal?(value) return if operation_produces_immutable_object?(value) return if frozen_string_literal?(value) return if shareable_constant_value?(value) add_offense(value) { |corrector| autocorrect(corrector, value) } end