LoginSignup
5
3

More than 3 years have passed since last update.

SwiftLint 0.40.0 で追加された computed_accessors_order に対応する

Posted at

SwiftLint-0.40.0 or later Xcode-11.6 Last Modified-2020/09/01

はじめに

先日、SwiftLintを0.40.1にアップデートしたところ、次のwarningが大量に出るようになったので対応方法をまとめました。

Computed Accessors Order Violation: Computed properties should declare first the getter and then the setter. (computed_accessors_order)

原因

SwiftLint 0.40.0で新たに追加されたcomputed_accessors_orderルールが原因でした。

Add computed_accessors_order rule to validate the order of get and set accessors in computed properties and subscripts.
Marcelo Fabri #3158
https://github.com/realm/SwiftLint/blob/master/CHANGELOG.md#0400-washable-mask

computed_accessors_order ルールの仕様

Computedプロパティのgetter/setterの順序を一貫させるためのルールで、0.40.0からはデフォルトで有効(getter→setterの順)になります。

  • Identifier: computed_accessors_order
  • Enabled by default: Enabled
  • Supports autocorrection: No
  • Kind: style
  • Analyzer rule: No
  • Minimum Swift compiler version: 4.1.0
  • Default configuration: warning, order: get_set

https://realm.github.io/SwiftLint/computed_accessors_order.html

対応方法

getter/setterの順序を統一していない場合

せっかくの機会なので、これを機にデフォルトの設定と同じgetter→setterの順で統一するのがいいと思います。
とはいえ、大量にgetter/setterがある場合は大変なので、computed_accessors_order.swiftlint.ymldisabled_rulesに追加することで、ルールを無効化することもできます。

swiftlint.yml
disabled_rules:
  - computed_accessors_order

getter/setterの順序を統一している場合

getter→setterの順

デフォルトの設定と同じ順序で統一できているため、特に何もしなくて大丈夫です。

setter→getterの順

デフォルトの設定と逆の順序のため、.swiftlint.ymlcomputed_accessors_orderorder: set_getを明示的に設定しましょう。

swiftlint.yml
computed_accessors_order:
  order: set_get

あとがき

将来的にautocorrectionもサポートされると嬉しいなーと思いました。1
技術に関して呟いているので、Twitter もフォローしてもらえると嬉しいです。

参考情報


  1. 現時点(2020/09/01)では未サポート 

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3