Rubocop/Hound 建议冻结字符串文字 class 名称

Rubocop/Hound recommend freezing string literal class names

我的项目使用 HoundCI 作为代码 linter,我相信它在内部使用了 rubocop。

最近我开始注意到这种警告 -

它出现在每个 class 定义中(例如 class User < ActiveRecord::Base)。

我理解冻结字符串文字的概念,但为什么它期望我冻结 class 定义?同样更重要的是,我该如何禁用它?有 10 多个 "errors" 污染了我们的 pull requests 是很烦人的。

谢谢!

编辑: 看起来它也开始出现在使用字符串文字的 require 语句中,例如 rspec 测试。这绝对是新的,之前没有被标记过

看起来 Hound/Rubocop 正在检测 FrozenStringLiteralComment 警察的违规行为。

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+.

您可以手动将魔法评论添加到文件顶部

# frozen_string_literal: true

或者让 Rubocop 为您完成

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

您也可以忽略 rubocop.ymlStyle/FrozenStringLiteralComment

中的警察