Skip to content

Ruby 3 support: Disable Style/RedundantFreeze cop

Matthias Käppler requested to merge mk-disable-redundantfreeze-cop into master

Ruby 3 does not auto-freeze interpolated strings anymore:

https://rubyreferences.github.io/rubychanges/3.0.html#interpolated-string-literals-are-no-longer-frozen-when--frozen-string-literal-true-is-used

# frozen-string-literal: true

def render(title, body)
  result = "## #{title}"
  result << "\n"
  result << body
end

puts render('The Title', 'The body')
# Ruby 2.7: in `render': can't modify frozen String (FrozenError)
# Ruby 3.0:
#   ## The Title
#   The body

But when I freeze the string with our current setup, I will trigger a Cop violation:

Style/RedundantFreeze: Do not freeze immutable objects, as freezing them has no effect.

We have several options here:

  • We disable the cop. Since we have another cop that checks for the file pragma, and since freezing interpolated strings isn't terribly useful anyway (since by definition they will change), this is a good option. Also, freezing an already frozen string does no harm.
  • We disable the cop on a per-case basis when freezing interpolated strings. This could become a hassle.
  • We keep the cop enabled, but never freeze interpolated strings. It is usually not useful, unless we anticipate that certain variants are instantiated very frequently per-process.

Merge request reports