Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rubocop/Hound recommend freezing string literal class names

My project uses HoundCI as a code linter, which I believe internally uses rubocop.

Recently I started noticing this sort of warning -

enter image description here

It appears on every class definition (e.g. class User < ActiveRecord::Base).

I understand the concept of freezing string literals, but why would it expect me to freeze class definitions? Also more importantly, how do I disable it? It's quite annoying to have 10+ of these "errors" polluting our pull requests.

Thank you!

Edit: Looks like it also started appearing on require statements that use string literals, like with rspec tests. This is definitely new and wasn't being flagged previously

enter image description here

like image 761
user2490003 Avatar asked Aug 31 '25 02:08

user2490003


1 Answers

It looks like Hound/Rubocop is detecting a violation of the FrozenStringLiteralComment cop.

This cop is designed to help upgrade to Ruby 3.0. It will add the comment # frozen_string_literal: true to the top of files to enable frozen string literals. Frozen string literals will be default in Ruby 3.0. The comment will be added below a shebang and encoding comment. The frozen string literal comment is only valid in Ruby 2.3+.

You can either add the magic comment manually to the top of your files

# frozen_string_literal: true

Or have Rubocop do it for you

$ bundle exec rubocop --auto-correct --only FrozenStringLiteralComment

You can also ignore the cop in your rubocop.yml, Style/FrozenStringLiteralComment

like image 68
messanjah Avatar answered Sep 02 '25 16:09

messanjah