2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SwiftLintをコメントで無視する

Last updated at Posted at 2021-06-15

はじめに

自分のメモのために記事を書いています。
初学者の方の参考になれば幸いです。

記事内容

SwiftLintをコードで無視するをメモとして書いています。
詳しい内容はGitHubのSwiftLint(Opt-In Rules)に書かれています。https://github.com/realm/SwiftLint

コードでルールを無視する

  • ルールは、次の形式のソースファイル内のコメントで無効にできます。
// swiftlint:disable <rule1> [<rule2> <rule3>...]
  • ルールは、ファイルの終わりまで、またはリンターが一致する有効化コメントを確認するまで無効になります。
// swiftlint:enable <rule1> [<rule2> <rule3>...]

例えば:

// swiftlint:disable colon
let noWarning :String = "" // 変数名の直後のコロンに関する警告はありません!

// swiftlint:enable colon
let hasWarning :String = "" // 変数名の直後のコロンについて警告が生成されます
  • allキーワードを含めると、リンターが一致する有効化コメントを確認するまで、すべてのルールが無効になります。
// swiftlint:disable all
// swiftlint:enable all

例えば:

// swiftlint:disable all
let noWarning :String = "" // 変数名の直後のコロンに関する警告はありません!
let i = "" // 短い識別子名についての警告もありません

// swiftlint:enable all
let hasWarning :String = "" // 変数名の直後のコロンについて警告が生成されます
let y = "" // 短い識別子名について警告が生成されます
  • 以下の三種類いずれかのコマンドを追加することで、有効範囲を変更することも可能です。
:previous
// 前の行のみコマンドを適用します
:this or :next
// この(現在の)行または次の行のみコマンドを適用します

例えば

// swiftlint:disable:next force_cast
let noWarning = NSNumber() as! Int
let hasWarning = NSNumber() as! Int
let noWarning2 = NSNumber() as! Int // swiftlint:disable:this force_cast
let noWarning3 = NSNumber() as! Int
// swiftlint:disable:previous force_cast
2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?