LoginSignup
146
131

More than 5 years have passed since last update.

SwiftLintのdisabled_rules一覧(実例付き)

Last updated at Posted at 2016-01-16

disabled_rulesには「これは無視してチェックしてください」という項目を指定します。
使用できるルールはswiftlint rulesコマンドで出力できます。
この記事ではそのそれぞれのルールについて実例を付けて説明したいと思います。

詳しく知りたい場合はライブラリの中身を見ると良いと思うので、下記ページをご覧ください。
https://github.com/realm/SwiftLint/tree/master/Source/SwiftLintFramework/Rules

なお、変数名の長さなど、長さが絡んでくるものに関してはカスタマイズできます。
ひとまずここではデフォルトでどのようになるかを記述していきます。

closing_brace

Closing Brace Spacing (closing_brace):
 Closing brace with closing parenthesis should not have any whitespaces in the middle.

スクリーンショット 2016-01-16 14.22.36.png

「}」と「)」の間にある空白を取り除けば警告は消えます。

colon

Colon (colon):
 Colons should be next to the identifier when specifying a type.

スクリーンショット 2016-01-16 14.39.19.png

「action:」と「UIAlertAction!」の間に空白文字を入れれば警告は消えます。

comma

Comma Spacing (comma):
 One space before and no after must be present next to any comma.

スクリーンショット 2016-01-16 14.42.15.png

「String,」と「message」の間に空白文字を入れれば警告は消えます。

control_statement

Control Statement (control_statement):
 if,for,while,do statements shouldn't wrap their conditionals in parentheses.

スクリーンショット 2016-01-16 14.48.36.png

条件文を囲んでいる「()」を取り除けば警告は消えます。

force_cast

Force Cast (force_cast):
 Force casts should be avoided.

スクリーンショット 2016-01-16 14.59.24.png

「as!」ではなく「as?」に変えたらビルドできるようになります。

force_try

Force Try (force_try):
 Force tries should be avoided.

スクリーンショット 2016-01-16 15.15.42.png

※isEqualStr()は例外を投げる可能性があるメソッド

きちんとtry-catchで囲めばビルドできるようになります。

leading_whitespace

Leading Whitespace (leading_whitespace):
 Files should not contain leading whitespace.

スクリーンショット 2016-01-16 15.19.31.png

ファイルの先頭の空白文字を削除すれば警告は消えます。

legacy_constructor

Legacy Constructor (legacy_constructor):
 Swift constructors are preferred over legacy convenience functions.

スクリーンショット 2016-01-16 15.42.39.png

CGPoint(x: 10, y: 10)にしたら警告は消えます。

nesting

Nesting (nesting):
 Types should be nested at most 1 level deep, and statements should be nested at most 5 levels deep.

スクリーンショット 2016-01-16 16.51.24.png

ネストを4つまでにすれば警告は消えます。

opening_brace

Opening Brace Spacing (opening_brace):
 Opening braces should be preceded by a single space and on the same line as the declaration.

スクリーンショット 2016-01-16 16.56.55.png

「{」の手前で改行しないでfunc func1() {とすれば警告は消えます。

operator_whitespace

Operator Function Whitespace (operator_whitespace):
 Operators should be surrounded by a single whitespace when defining them.

スクリーンショット 2016-01-16 19.51.23.png

スクリーンショット 2016-01-16 19.53.05.png

スクリーンショット 2016-01-16 19.53.37.png

前後を空白文字1文字にすれば警告は消えます。
(オペレータを定義するときに前や後ろに2つ以上の空白文字や空白文字が1つもないと警告が出ます)

return_arrow_whitespace

Returning Whitespace (return_arrow_whitespace):
 Return arrow and return type should be separated by a single space or on a separate line.

スクリーンショット 2016-01-16 17.15.20.png

「->」の前後に空白文字を入れれば警告は消えます。

statement_position

Statement Position (statement_position):
 Else and catch should be on the same line, one space after the previous declaration.

スクリーンショット 2016-01-16 17.20.55.png

「else」の前に1つ空白を入れれば警告は消えます。

todo

Todo (todo):
 TODOs and FIXMEs should be avoided.

スクリーンショット 2016-01-16 17.23.39.png

TODO, FIXMEがなくなれば警告は消えます。

trailing_newline

Trailing Newline (trailing_newline):
 Files should have a single trailing newline.

スクリーンショット 2016-01-16 17.26.31.png

ファイルの最後に空白行を入れれば警告は消えます。

trailing_semicolon

Trailing Semicolon (trailing_semicolon):
 Lines should not have trailing semicolons.

スクリーンショット 2016-01-16 17.27.58.png

セミコロンを消せば警告は消えます。

trailing_whitespace

Trailing Whitespace (trailing_whitespace):
 Lines should not have trailing whitespace.

スクリーンショット 2016-01-16 17.31.55.png

super.viewDidLoad()の後ろには空白スペースが入っています。

行末の空白文字、何も記述されていない行を消せば警告は消えます。

type_name

Type Name (type_name):
 Type name should only contain alphanumeric characters, start with an uppercase character and span
 between 3 and 40 characters in length.

スクリーンショット 2016-01-16 17.40.06.png

スクリーンショット 2016-01-16 17.41.16.png

スクリーンショット 2016-01-16 17.40.42.png

タイプ名を

  • 英数字のみ
  • 3文字以上40文字以下
  • 先頭は大文字

にすれば警告が消え、ビルドができるようになります。

valid_docs

Valid Docs (valid_docs):
 Documented declarations should be valid.

これらは Docs とメソッドの内容の不整合によって警告が出ます。

スクリーンショット 2016-01-16 18.03.40.png
下記の二通りの方法で警告が消えます。
* メソッドの定義が正しい場合: コメントの - returns を削除
* コメントが正しい場合: 戻り値があるメソッドに修正

スクリーンショット 2016-01-16 18.07.58.png

ドキュメントコメントに書いたものと同じ引数(str)を取るようにすれば警告は消えます。

スクリーンショット 2016-01-16 18.12.38.png

「/** */」で囲んだらコメントが間違っている云々以前に、何も書いてなくても警告が出るようです。

variable_name

Variable Name (variable_name):
 Variable name should only contain alphanumeric characters and start with a lowercase character 
 or should only contain capital letters.
 In an exception to the above, variable names may start with a capital letter 
 when they are declared static and immutable.

スクリーンショット 2016-01-16 18.34.33.png

  • 英数字だけ
  • 小文字のみか大文字のみ

の名前に変更すれば警告は消えます。

file_length

File Line Length (file_length):
 Files should not span too many lines.

スクリーンショット 2016-01-16 18.37.49.png

スクリーンショット 2016-01-16 18.38.18.png

ファイルが401〜1000行だと警告が出て、1001行以上だとビルドエラーになります。
なので400行以内に抑えればビルドエラーも警告もでません。

function_body_length

Function Body Length (function_body_length):
 Functions bodies should not span too many lines.

スクリーンショット 2016-01-16 18.44.43.png

スクリーンショット 2016-01-16 18.45.06.png

メソッドが41〜100行だと警告が出て、101行以上だとビルドエラーになります。
なので40行以内に抑えればビルドエラーも警告もでません。

line_length

Line Length (line_length):
 Lines should not span too many characters.

スクリーンショット 2016-01-16 18.53.46.png

スクリーンショット 2016-01-16 18.55.02.png

1行が101〜200文字だと警告が出て、201文字以上だとビルドエラーになります。
なので100文字以内に抑えればビルドエラーも警告もでません。

type_body_length

Type Body Length (type_body_length):
 Type bodies should not span too many lines.

スクリーンショット 2016-01-16 18.59.47.png

スクリーンショット 2016-01-16 19.00.11.png

Class, Struct, Enumの中身が201〜350行だと警告が出て、351行以上だとビルドエラーになります。
なので200行以内に抑えればビルドエラーも警告もでません。

variable_name_max_length

Variable Name Max Length Rule (variable_name_max_length):
 Variable name should not be too long.

スクリーンショット 2016-01-16 19.40.08.png

スクリーンショット 2016-01-16 19.41.23.png

変数、定数名が41〜60文字だと警告が出て、61行以上だとビルドエラーになります。
なので40字以内に抑えればビルドエラーも警告もでません。

variable_name_min_length

Variable Name Min Length Rule (variable_name_min_length):
 Variable name should not be too short.

スクリーンショット 2016-01-16 19.44.03.png

スクリーンショット 2016-01-16 19.43.44.png

変数、定数名が2文字だと警告が出て、1文字だとビルドエラーになります。
なので3文字以上にすればビルドエラーも警告もでません。

146
131
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
146
131