はじめに
先日、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.ymlのdisabled_rulesに追加することで、ルールを無効化することもできます。
disabled_rules:
- computed_accessors_order
getter/setterの順序を統一している場合
getter→setterの順
デフォルトの設定と同じ順序で統一できているため、特に何もしなくて大丈夫です。
setter→getterの順
デフォルトの設定と逆の順序のため、.swiftlint.ymlにcomputed_accessors_orderのorder: set_getを明示的に設定しましょう。
computed_accessors_order:
order: set_get
あとがき
将来的にautocorrectionもサポートされると嬉しいなーと思いました。1
技術に関して呟いているので、Twitter もフォローしてもらえると嬉しいです。
参考情報
-
現時点(2020/09/01)では未サポート ↩