Tech Racho エンジニアの「?」を「!」に。
  • Ruby / Rails関連

Rails: 最近のRuboCop更新をrubocop.ymlで有効にした

自分のRails環境でしばらくぶりにrubocop -aをかけてみたところ、以下のメッセージがどどどっと表示されました。

The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file:
 - Layout/EmptyLinesAroundAttributeAccessor (0.83)
 - Layout/SpaceAroundMethodCallOperator (0.82)
 - Lint/DeprecatedOpenSSLConstant (0.84)
 - Lint/DuplicateElsifCondition (0.88)
 - Lint/MixedRegexpCaptureTypes (0.85)
 - Lint/RaiseException (0.81)
 - Lint/StructNewOverride (0.81)
 - Style/AccessorGrouping (0.87)
 - Style/ArrayCoercion (0.88)
 - Style/BisectedAttrAccessor (0.87)
 - Style/CaseLikeIf (0.88)
 - Style/ExponentialNotation (0.82)
 - Style/HashAsLastArrayItem (0.88)
 - Style/HashLikeCase (0.88)
 - Style/RedundantAssignment (0.87)
 - Style/RedundantFetchBlock (0.86)
 - Style/RedundantFileExtensionInRequire (0.88)
 - Style/RedundantRegexpCharacterClass (0.85)
 - Style/RedundantRegexpEscape (0.85)
 - Style/SlicingWithRange (0.83)
 - Performance/AncestorsInclude (1.7)
 - Performance/BigDecimalWithNumericArgument (1.7)
 - Performance/RedundantSortBlock (1.7)
 - Performance/RedundantStringChars (1.7)
 - Performance/ReverseFirst (1.7)
 - Performance/SortReverse (1.7)
 - Performance/Squeeze (1.7)
 - Performance/StringInclude (1.7)
 - Rails/ActiveRecordCallbacksOrder (2.7)
 - Rails/FindById (2.7)
 - Rails/Inquiry (2.7)
 - Rails/MailerName (2.7)
 - Rails/MatchRoute (2.7)
 - Rails/NegateInclude (2.7)
 - Rails/Pluck (2.7)
 - Rails/PluckInWhere (2.7)
 - Rails/RenderInline (2.7)
 - Rails/RenderPlainText (2.7)
 - Rails/ShortI18n (2.7)
 - Rails/WhereExists (2.7)
For more information: https://docs.rubocop.org/rubocop/versioning.html
# (略)

なお、10日ほど後にもう一度かけてみるとさらに追加されていました。0.89で追加されたものにはunsafeなcopが多めだったようです。

The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file:
 - Lint/BinaryOperatorWithIdenticalOperands (0.89)
 - Lint/DuplicateRescueException (0.89)
 - Lint/EmptyConditionalBody (0.89)
 - Lint/FloatComparison (0.89)
 - Lint/MissingSuper (0.89)
 - Lint/OutOfRangeRegexpRef (0.89)
 - Lint/SelfAssignment (0.89)
 - Lint/TopLevelReturnWithArgument (0.89)
 - Lint/UnreachableLoop (0.89)
 - Style/ExplicitBlockArgument (0.89)
 - Style/GlobalStdStream (0.89)
 - Style/OptionalBooleanParameter (0.89)
 - Style/SingleArgumentDig (0.89)
 - Style/StringConcatenation (0.89)
For more information: https://docs.rubocop.org/rubocop/versioning.html

一部では何ごとかとissueを投げていた人もいたようですが、これはRuboCop 0.79#7567)で導入されたpendingステータスに関連するメッセージなのだそうです。

参考: RuboCopの"The following cops were added to RuboCop, but are not configured."の対処方法 - Qiita

逆に言うと、RuboCopを更新しても基本的に新しいcopはひとりでには有効にならなくなったということなので、どうやら新しいcopを使うには自分でrubocop.yamlに反映しないといけなさそうです。

チームのrubocop.ymlだと勝手なことはできませんが、自分の環境なので以下のサイトを参考に遠慮なくrubocop.ymlを更新しました。

yamlにちまちま移し替えるのが割と面倒だったので、もったいない精神で記事にもyamlの追加部分を貼り付けてみました。すべてEnable: trueにしてあるので、コピペする方は適宜falseにするなどしてください。

なお、RuboCopドキュメントで「Safe: yes」と書いてあっても、yamlにSafe: trueと書くとwarningが表示されるcopがかなりあったので、Safe: trueはwarningが出ないものだけ書いてあります。

Layout部門のcop

クリックしてyamlを表示
Layout/EmptyLinesAroundAttributeAccessor:
  Description: "Checks for a newline after an attribute accessor or a group of them."
  Enabled: true
  VersionAdded: "0.83"
  VersionChanged: "0.84"

Layout/SpaceAroundMethodCallOperator:
  Description: "Checks method call operators to not have spaces around them."
  Enabled: true
  VersionAdded: "0.82"

Lint部門のcop

クリックしてyamlを表示
Lint/DeprecatedOpenSSLConstant:
  Description: "Algorithmic constants for OpenSSL::Cipher and OpenSSL::Digest deprecated since OpenSSL version 2.2.0. Prefer passing a string instead."
  Enabled: true
  VersionAdded: "0.84"

Lint/DuplicateElsifCondition:
  Description: "This cop checks that there are no repeated conditions used in if 'elsif'."
  Enabled: true
  VersionAdded: "0.88"

Lint/MixedRegexpCaptureTypes:
  Description: "Do not mix named captures and numbered captures in a Regexp literal because numbered capture is ignored if they’re mixed."
  Enabled: true
  VersionAdded: "0.85"

Lint/RaiseException:
  Description: "This cop checks for raise or fail statements which are raising Exception class."
  Enabled: true
  Safe: true
  VersionAdded: "0.81"

Lint/StructNewOverride:
  Description: "This cop checks unexpected overrides of the Struct built-in methods via Struct.new."
  Enabled: true
  VersionAdded: "0.81"

Lint/BinaryOperatorWithIdenticalOperands:
  Description: "This cop checks for places where binary operator has identical operands."
  Enabled: true
  VersionAdded: "0.89"

Lint/DuplicateRescueException:
  Description: "This cop checks that there are no repeated exceptions used in 'rescue' expressions."
  Enabled: true
  VersionAdded: "0.89"

Lint/EmptyConditionalBody:
  Description: "This cop checks for the presence of if, elsif and unless branches without a body."
  Enabled: true
  VersionAdded: "0.89"

Lint/FloatComparison:
  Description: "This cop checks for the presence of precise comparison of floating point numbers."
  Enabled: true
  VersionAdded: "0.89"

Lint/MissingSuper:
  Description: "This cop checks for the presence of constructors and lifecycle callbacks without calls to super."
  Enabled: true
  VersionAdded: "0.89"

Lint/OutOfRangeRegexpRef:
  Description: "This cops looks for references of Regexp captures that are out of range and thus always returns nil."
  Enabled: true
  VersionAdded: "0.89"

Lint/SelfAssignment:
  Description: "This cop checks for self-assignments."
  Enabled: true
  VersionAdded: "0.89"

Lint/TopLevelReturnWithArgument:
  Description: "This cop checks for top level return with arguments. If there is a top-level return statement with an argument, then the argument is always ignored. This is detected automatically since Ruby 2.7."
  Enabled: true
  VersionAdded: "0.89"

Lint/UnreachableLoop:
  Description: "This cop checks for loops that will have at most one iteration."
  Enabled: true
  VersionAdded: "0.89"

Style部門のcop

クリックしてyamlを表示
Style/AccessorGrouping:
  Description: "This cop checks for grouping of accessors in class and module bodies."
  Enabled: true
  VersionAdded: "0.87"

Style/ArrayCoercion:
  Description: "This cop enforces the use of Array() instead of explicit Array check or [*var]."
  Enabled: true
  VersionAdded: "0.88"

Style/BisectedAttrAccessor:
  Description: "This cop checks for places where attr_reader and attr_writer for the same method can be combined into single attr_accessor."
  Enabled: true
  VersionAdded: "0.87"

Style/CaseLikeIf:
  Description: "This cop identifies places where if-elsif constructions can be replaced with case-when."
  Enabled: true
  VersionAdded: "0.88"

Style/ExponentialNotation:
  Description: "This cop enforces consistency when using exponential notation for numbers in the code (eg 1.2e4)."
  Enabled: true
  VersionAdded: "0.82"

Style/HashAsLastArrayItem:
  Description: "Checks for presence or absence of braces around hash literal as a last array item depending on configuration."
  Enabled: true
  VersionAdded: "0.88"

Style/HashLikeCase:
  Description: "This cop checks for places where case-when represents a simple 1:1 mapping and can be replaced with a hash look"
  Enabled: true
  VersionAdded: "0.88"

Style/RedundantAssignment:
  Description: "This cop checks for redundant assignment before returning."
  Enabled: true
  VersionAdded: "0.87"

Style/RedundantFetchBlock:
  Description: "This cop identifies places where fetch(key) { value } can be replaced by fetch(key, value)"
  Enabled: true
  Safe: false
  VersionAdded: "0.86"

Style/RedundantFileExtensionInRequire:
  Description: "This cop checks for the presence of superfluous .rb extension in the filename provided to require and require_relative."
  Enabled: true
  VersionAdded: "0.88"

Style/RedundantRegexpCharacterClass:
  Description: "This cop checks for unnecessary single-element Regexp character classes."
  Enabled: true
  VersionAdded: "0.85"

Style/RedundantRegexpEscape:
  Description: "This cop checks for redundant escapes inside Regexp literals."
  Enabled: true
  VersionAdded: "0.85"

Style/SlicingWithRange:
  Description: "This cop checks that arrays are sliced with endless ranges instead of ary[start..-1] on Ruby 2.6+."
  Enabled: true
  Safe: false
  VersionAdded: "0.83"

Style/ExplicitBlockArgument:
  Description: "This cop enforces the use of explicit block argument to avoid writing block literal that just passes its arguments to another block."
  Enabled: true
  VersionAdded: "0.89"

Style/GlobalStdStream:
  Description: "This cop enforces the use of $stdout/$stderr/$stdin instead of STDOUT/STDERR/STDIN. STDOUT/STDERR/STDIN are constants, and while you can actually reassign (possibly to redirect some stream) constants in Ruby, you’ll get an interpreter warning if you do so."
  Enabled: true
  VersionAdded: "0.89"

Style/OptionalBooleanParameter:
  Description: "This cop checks for places where keyword arguments can be used instead of boolean arguments when defining methods."
  Enabled: true
  VersionAdded: "0.89"

Style/SingleArgumentDig:
  Description: "Sometimes using dig method ends up with just a single argument. In such cases, dig should be replaced with []."
  Enabled: true
  VersionAdded: "0.89"

Style/StringConcatenation:
  Description: "This cop checks for places where string concatenation can be replaced with string interpolation."
  Enabled: true
  VersionAdded: "0.89"

Performance部門のcop

クリックしてyamlを表示
#################### Performance ###############################

Performance/AncestorsInclude:
  Description: "This cop is used to identify usages of ancestors.include? and change them to use ⇐ instead."
  Enabled: true
  Safe: false
  VersionAdded: "1.7"

Performance/BigDecimalWithNumericArgument:
  Description: "This cop identifies places where numeric argument to BigDecimal should be converted to string. Initializing from String is faster than from Numeric for BigDecimal."
  Enabled: true
  VersionAdded: "1.7"

Performance/RedundantSortBlock:
  Description: "This cop identifies places where sort { |a, b| a <⇒ b } can be replaced with sort."
  Enabled: true
  VersionAdded: "1.7"

Performance/RedundantStringChars:
  Description: "This cop checks for redundant String#chars."
  Enabled: true
  VersionAdded: "1.7"

Performance/ReverseFirst:
  Description: "This cop identifies places where reverse.first(n) and reverse.first can be replaced by last(n).reverse and last."
  Enabled: true
  VersionAdded: "1.7"

Performance/SortReverse:
  Description: "This cop identifies places where sort { |a, b| b <⇒ a } can be replaced by a faster sort.reverse."
  Enabled: true
  VersionAdded: "1.7"

Performance/Squeeze:
  Description: "This cop identifies places where gsub(/a+/, 'a') and gsub!(/a+/, 'a') can be replaced by squeeze('a') and squeeze!('a')."
  Enabled: true
  VersionAdded: "1.7"

Performance/StringInclude:
  Description: "This cop identifies unnecessary use of a regex where String#include? would suffice."
  Enabled: true
  AutoCorrect: true
  VersionAdded: "1.7"

Rails部門のcop

クリックしてyamlを表示
#################### Rails #####################################

Rails/ActiveRecordCallbacksOrder:
  Description: "This cop checks that Active Record callbacks are declared in the order in which they will be executed."
  Enabled: true
  VersionAdded: "2.7"

Rails/FindById:
  Description: "This cop enforces that ActiveRecord#find is used instead of where.take!, find_by!, and find_by_id! to retrieve a single record by primary key when you expect it to be found."
  Enabled: true
  VersionAdded: "2.7"

Rails/Inquiry:
  Description: "This cop checks that Active Support’s inquiry method is not used."
  Enabled: true
  VersionAdded: "2.7"

Rails/MailerName:
  Description: "This cop enforces that mailer names end with Mailer suffix."
  Enabled: true
  VersionAdded: "2.7"

Rails/MatchRoute:
  Description: "This cop identifies places where defining routes with match can be replaced with a specific HTTP method."
  Enabled: true
  VersionAdded: "2.7"

Rails/NegateInclude:
  Description: "This cop enforces the use of collection.exclude?(obj) over !collection.include?(obj)."
  Enabled: true
  VersionAdded: "2.7"

Rails/Pluck:
  Description: "This cop enforces the use of pluck over map."
  Enabled: true
  VersionAdded: "2.7"

Rails/PluckInWhere:
  Description: "This cop identifies places where pluck is used in where query methods and can be replaced with select."
  Enabled: true
  VersionAdded: "2.7"

Rails/RenderInline:
  Description: "This cop looks for inline rendering within controller actions."
  Enabled: true
  VersionAdded: "2.7"

Rails/RenderPlainText:
  Description: "This cop identifies places where render text: can be replaced with render plain:."
  Enabled: true
  ContentTypeCompatibility: true
  VersionAdded: "2.7"

Rails/ShortI18n:
  Description: "This cop checks for consistent uses of request.referer or request.referrer, depending on the cop’s configuration."
  Enabled: true
  EnforcedStyle: "conservative"
  VersionAdded: "2.7"

Rails/WhereExists:
  Description: "This cop enforces the use of exists?(…​) over where(…​).exists?."
  Enabled: true
  VersionAdded: "2.7"

以上を反映して動作を確認できたら、念のためmry↓も実行して、古い部署名が残ってないことを確認します。

参考: RuboCop の Cop 名をマイグレーションする - koicの日記

今後のために、上の記事に従って既存のMigration/DepartmentNameも有効にしておきましょう(ドキュメントではデフォルトで有効となっていますが、私の.rubocop.ymlが古かったせいでオフになっていました)。

################## Migration #############################

Migration/DepartmentName:
  Description: >-
    Check that cop names in rubocop:disable (etc) comments are
    given with department name.
  Enabled: true

おまけ: EnabledByDefault

ここまで記事を書いた後で、ruby-jp SlackでEnabledByDefaultという設定があることを知りました。

AllCops:
  EnabledByDefault: false

この設定をtrueにすると、個別に無効にしたcopも含めてすべて有効になります(デフォルトはfalse)。プロジェクトでは普段falseのままにしておいて、最近どんなcopが増えたか知りたいと思ったときに一時的にtrueにするとよさそうです。

実際にEnabledByDefault: trueにしてみると、以下のConstantResolutionという新しいcopが上のwarningに含まれていなかったことに気づいたので、一応rubocop.ymlに追加しましたが、デフォルトがfalseなので、これだけはfalseにしておきました。 only:ignore:でチェック対象の定数名を絞り込んで使う前提のようで、そうした設定なしでこのcopをおそるおそるtrueにするとほとんどの定数名で大量の怒られが発生しました。

Lint/ConstantResolution:
  Description: "Check that certain constants are fully qualified."
  Enabled: false
  VersionAdded: "0.86"

ところで、RuboCopのドキュメントにyaml形式の設定も表示されていたらそのままyamlにコピペできて便利なのにと思いました。

関連記事

docker-composeを便利にするツール「dip」を使ってみた


CONTACT

TechRachoでは、パートナーシップをご検討いただける方からの
ご連絡をお待ちしております。ぜひお気軽にご意見・ご相談ください。