LoginSignup
21
7

SwiftLintで追加・廃止されたルールまとめ(Swift 5.1.2→5.3.1版)

Last updated at Posted at 2020-12-06

SwiftLintの記事はシリーズになっています。
記事を順番に読み進めると、SwiftLintを使いこなせるようになります。

はじめに

本記事は Swift/Kotlin愛好会 Advent Calendar 2020 の7日目の記事です。

SwiftLintで追加・変更・廃止されたルールまとめ(Swift 4.2→5.1.2版) - Qiita から2020/12/06現在の最新版までの間に、追加・廃止されたルールを解説します。
前回の記事を読んでいなくても読み進められますが、併せて読むことをオススメします。

比較方法

前回の記事や公式ドキュメント、リリースノートを参考に差分を抽出しました。
変更されたルールも書こうとしたのですが、リリースノートを見ると多かったので見送りました。

ルール
追加 13
廃止 0

※ソースでなくドキュメントを確認して調査したため、実際の数と異なる可能性があります。

環境

  • OS:macOS Big Sur 11.0.1
  • Swift:5.3.1
  • Xcode:12.2 (12B45b)
  • SwiftLint:0.42.0-rc.1
    0.42.0は正式リリースされていない

追加されたルール

Comment Spacing

  • 追加バージョン:0.42.0-rc.1
  • デフォルト:有効

コメントはスラッシュの後ろにスペースを1つ以上空けるべきです。
https://realm.github.io/SwiftLint/comment_spacing.html

// bad
//Foo
///Foo

// good
// Foo
//  Foo
/// Foo

Computed Accessors Order

  • 追加バージョン:0.40.0
  • デフォルト:有効

コンピューテッドプロパティのゲッターとセッターは、統一した順序にすべきです。
https://realm.github.io/SwiftLint/computed_accessors_order.html

// bad
class Foo {
    var foo: Int {
        set { _foo = newValue }
        get { 3 }
    }
}

// good
class Foo {
    var foo: Int {
        get { 3 }
        set { _foo = newValue }
    }
}

Inclusive Language

  • 追加バージョン:0.41.0
  • デフォルト:有効

識別子は人種、性別、社会経済的地位に基づく差別を避けるために、包括的な言語を使うべきです。
https://realm.github.io/SwiftLint/inclusive_language.html

// bad
enum BlackList {
    case foo
    case bar
}

func updateWhiteList(add: String) {}

init(master: String, slave: String) {}

// good
enum DenyList {
    case foo
    case bar
}

func updateAllowList(add: String) {}

init(leader: String, follower: String) {}

enum WalletItemType {
    case visa
    case mastercard // `0.42.0-rc.1` からデフォルトで許可された
}

Twitterが提供している一覧が参考になります。

Orphaned Doc Comment

  • 追加バージョン:0.38.2
  • デフォルト:有効

ドキュメンテーションコメントは宣言に付けるべきです。
https://realm.github.io/SwiftLint/orphaned_doc_comment.html

// bad
/// My great property
// Not a doc string
var myGreatProperty: String!

// good
/// My great property
var myGreatProperty: String!

Enum Case Associated Values Count

  • 追加バージョン:0.38.1
  • デフォルト:無効

列挙型のアソシエイテッドバリューは少なくすべきです。
https://realm.github.io/SwiftLint/enum_case_associated_values_count.html

// bad
enum Employee {
    case fullTime(name: String, retirement: Date, age: Int, designation: String, contactNumber: Int)
    case partTime(name: String, contractEndDate: Date, age: Int, designation: String, contactNumber: Int)
}

// good
enum Employee {
    case fullTime(name: String, retirement: Date, designation: String, contactNumber: Int)
    case partTime(name: String, age: Int, contractEndDate: Date)
}

IBInspectable in Extension

  • 追加バージョン:0.40.0
  • デフォルト:無効

エクステンションで @IBInspectable のプロパティを追加すべきではありません。
https://realm.github.io/SwiftLint/ibinspectable_in_extension.html

// bad
extension Foo {
  @IBInspectable private var x: Int
}

// good
class Foo {
  @IBInspectable private var x: Int
}

Indentation Width

  • 追加バージョン:0.38.2
  • デフォルト:無効

1つのタブまたは設定したスペースの量でコードをインデントすべきです。
https://realm.github.io/SwiftLint/indentation_width.html

// bad
firstLine
        secondLine

// good
firstLine
    secondLine

Non-private XCTest member

  • 追加バージョン:0.42.0-rc.1
  • デフォルト:無効

テストクラスのテストでないメンバーはすべて private にすべきです。
https://realm.github.io/SwiftLint/non_private_xctest_member.html

// bad
class TotoTests: XCTestCase {
    var foo: Bar?

    func helperFunction() {}
}

// good
class TotoTests: XCTestCase {
    private var foo: Bar?

    private func helperFunction() {}
}

Optional Enum Case Match

  • 追加バージョン:0.38.1
  • デフォルト:無効

オプショナルの列挙型は ? を付けずにマッチすべきです。
https://realm.github.io/SwiftLint/optional_enum_case_matching.html

// bad
switch foo {
 case .bar?: break
 case .baz: break
 default: break
}

// good
switch foo {
 case .bar: break
 case .baz: break
 default: break
}

Prefer Nimble

  • 追加バージョン:0.41.0
  • デフォルト:無効

XCTAssert よりNimbleのマッチャーを使うべきです。
https://realm.github.io/SwiftLint/prefer_nimble.html

// bad
XCTAssertEqual(foo, 1)

// good
expect(foo) == 1

Prefer Self Type Over Type of Self

  • 追加バージョン:0.38.1
  • デフォルト:無効

type(of: self) より Self を使うべきです。
https://realm.github.io/SwiftLint/prefer_self_type_over_type_of_self.html

// bad
class Foo {
    func bar() {
        type(of: self).baz()
    }
}

// good
class Foo {
    func bar() {
        Self.baz()
    }
}

Prefer Zero Over Explicit Init

  • 追加バージョン:0.40.0
  • デフォルト:無効

CGPoint(x: 0, y: 0) のようなパラメータが 0 のイニシャライザより .zero を使うべきです。
https://realm.github.io/SwiftLint/prefer_zero_over_explicit_init.html

// bad
CGPoint(x: 0.0, y: 0.0)
CGRect(x: 0, y: 0, width: 0, height: 0)
CGSize(width: 0, height: 0)
CGVector(dx: 0, dy: 0)

// good
.zero
.zero
.zero
.zero

Test case accessibility

  • 追加バージョン:0.41.0
  • デフォルト:無効

テストケースにはテスト以外の private なメンバーのみ含まれるべきです。
https://realm.github.io/SwiftLint/test_case_accessibility.html

// bad
class FooTests: XCTestCase {
    var foo: String?

    func foobar() {}
}

// good
class FooTests: XCTestCase {
    private var foo: String?

    private func foobar() {}
}

「Non-private XCTest member」との違いがまだわかっていません。

廃止されたルール

なし

おわりに

これでSwift 5.3.1でもSwiftLintを適切に使うことができます!

以上、 Swift/Kotlin愛好会 Advent Calendar 2020 の7日目の記事でした。
明日も @uhooi の記事です。

参考リンク

21
7
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
21
7