class RuboCop::Cop::Style::MutableConstant
This cop checks whether some constant value isn't a mutable literal (e.g. array or hash).
@example
# bad CONST = [1, 2, 3] # good CONST = [1, 2, 3].freeze
Constants
- MSG
Public Instance Methods
on_casgn(node)
click to toggle source
# File lib/rubocop/cop/style/mutable_constant.rb, line 20 def on_casgn(node) _scope, _const_name, value = *node on_assignment(value) end
on_or_asgn(node)
click to toggle source
# File lib/rubocop/cop/style/mutable_constant.rb, line 25 def on_or_asgn(node) lhs, value = *node return unless lhs && lhs.casgn_type? on_assignment(value) end
Private Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/mutable_constant.rb, line 45 def autocorrect(node) expr = node.source_range lambda do |corrector| if node.array_type? && !node.bracketed? corrector.insert_before(expr, '[') corrector.insert_after(expr, '].freeze') else corrector.insert_after(expr, '.freeze') end end end
on_assignment(value)
click to toggle source
# File lib/rubocop/cop/style/mutable_constant.rb, line 35 def on_assignment(value) value = splat_value(value) if splat_value(value) return unless value && value.mutable_literal? return if FROZEN_STRING_LITERAL_TYPES.include?(value.type) && frozen_string_literals_enabled? add_offense(value) end