SwiftLint Rule 速览

SwiftLint 是一个很好的 lint 工具, 我简单查看记录了 lint 的规则, 方便记忆和查看
更具体的规则使用 swiftlint rules _rulename_ 查看

| identifier                              | opt-in | correctable | enabled in your config
----------------------+

| 以下是可选规则, 需要额外添加
| attributes                              | yes    | no          | no    Attributes 针对类和func重启一行, 针对变量在同一行
| closure_end_indentation                 | yes    | no          | no    闭包的 } 与 { 所在行的缩进要相同
| closure_spacing                         | yes    | no          | no    闭包应该与括号有间隔                
| conditional_returns_on_newline          | yes    | no          | no    条件判断语句需要重启一行 return                
| empty_count                             | yes    | no          | no    容器 使用 isEmpty 代替 count 判断是否为空               
| explicit_type_interface                 | yes    | no          | no    需要跑明确参数的类型定义                
| fatal_error_message                     | yes    | no          | no    fatalError 必须拥有一个 message                 
| file_header                             | yes    | no          | no    file header 的限制
| first_where                             | yes    | no          | no    使用 `.first(where:)` 代替 `.filter { }.first`
| force_unwrapping                        | yes    | no          | no    避免强制解包
| implicitly_unwrapped_optional           | yes    | no          | no    避免隐式解包(定义 ! 类型)                 
| valid_docs                              | yes    | no          | no    文档声明应该是正确有效的
| missing_docs                            | yes    | no          | no    避免遗失文档                 
| sorted_imports                          | yes    | no          | no    引用需要按照字母排序                 
| switch_case_on_newline                  | yes    | no          | no    switch 的 case 需要新启一行              
| object_literal                          | yes    | no          | no    避免 image and color 使用字面量初始化, 需要把相关图片名,颜色RGB 等参数定义为 enum struct 或者常量                          
| overridden_super_call                   | yes    | no          | no    override 方法需要调用 super method                 
| private_outlet                          | yes    | no          | no    IBOutlets 应该设置为 private, 来避免泄露
| prohibited_super_call                   | yes    | no          | no    某些特殊的 override 方法, 禁止调用 super method                 
| redundant_nil_coalescing                | yes    | yes         | no    避免使用 `object ?? nil` 
| explicit_init                           | yes    | yes         | no    避免直接调用 init 方法
| operator_usage_whitespace               | yes    | yes         | no    操作符需要使用一个空格间隔
| nimble_operator                         | yes    | yes         | no    避免 expect 一个确定的判断                 
| number_separator                        | yes    | yes         | no    使用 _ 分割大数, 让数字更清晰    

| 以下是默认规则             
| empty_parameters                        | no     | yes         | yes   使用 `() -> ` 代替 `Void ->                 
| empty_parentheses_with_trailing_closure | no     | yes         | yes   尾闭包避免空参数括号             
| colon                                   | no     | yes         | yes   冒号左边没有空格, 右边有空格                 
| comma                                   | no     | yes         | yes   逗号左边没有空格, 右边有空格   
| leading_whitespace                      | no     | yes         | yes   文件末尾不应该存在空格符                 
| legacy_cggeometry_functions             | no     | yes         | yes   避免使用 C 风格 的 CG 遗留函数, 使用 struct extension                
| legacy_constant                         | no     | yes         | yes   避免使用 遗留的全局常量, 使用 struct 内定义的 常量                 
| legacy_constructor                      | no     | yes         | yes   使用 swift 提供的 struct 构造函数, 避免使用 遗留的构造函数 比如 CGPointMake(10, 10)                
| legacy_nsgeometry_functions             | no     | yes         | yes   避免使用 C 风格 的 NS 遗留函数, 使用 struct extension   
| mark                                    | no     | yes         | yes   正确使用 mark 的格式 `// MARK: - message`                 
| opening_brace                           | no     | yes         | yes   需要正确书写大括号格式                 
| redundant_discardable_let               | no     | yes         | yes   使用 `_ = foo()` 代替 `let _ = foo()`                 
| redundant_optional_initialization       | no     | yes         | yes   不需要写默认值为 nil   
| closing_brace                           | no     | yes         | yes   小括号内包含函数(大括号)的时候,之间没有空格                 
| redundant_void_return                   | no     | yes         | yes   在不必要的时候, 不需要写 ->() and -> Void                 
| return_arrow_whitespace                 | no     | yes         | yes   函数定义返回的 ->  前后有空格, 不换行 
| statement_position                      | no     | yes         | yes   else and catch 应该与 } 在同一行, 以空格间隔
| trailing_newline                        | no     | yes         | yes   文件末尾应该有一个空行
| trailing_semicolon                      | no     | yes         | yes   行末尾不加分号
| trailing_whitespace                     | no     | yes         | yes   行末尾不加空格
| unused_closure_parameter                | no     | yes         | yes   函数的参数必须被使用
| vertical_whitespace                     | no     | yes         | yes   不能有连续多个空行
| void_return                             | no     | yes         | yes   使用 `-> Void` 代替 `-> ()

| class_delegate_protocol                 | no     | no          | yes   delegate protocol 应该被设定为 class-only                 
| closure_parameter_position              | no     | no          | yes   闭包的参数应该和 { 在同一行                           
| compiler_protocol_init                  | no     | no          | yes   不应该直接调用字面量转换的初始化方法                  
| control_statement                       | no     | no          | yes   if while 等判断条件不要用括号 括起来                 
| custom_rules                            | no     | no          | yes   一些自定义规则                 
| cyclomatic_complexity                   | no     | no          | yes   不应该存在太复杂的函数(判断语句过多)                 
| discarded_notification_center_observer  | no     | no          | yes   当使用 block 注册通知中心 observer 的时候, 应该存储函数返回的 observer, 以便之后的删除
| file_length                             | no     | no          | yes   文件长度限制                 
| for_where                               | no     | no          | yes   使用 `for where` 代替 简单的 `for { if }`                 
| force_cast                              | no     | no          | yes   避免强制的类型转化                 
| force_try                               | no     | no          | yes   避免 `try!`                 
| function_body_length                    | no     | no          | yes   body 长度限制                 
| function_parameter_count                | no     | no          | yes   函数参数数目限制                 
| generic_type_name                       | no     | no          | yes   类型命名规则限制                
| identifier_name                         | no     | no          | yes   参数变量命名规则                 
| implicit_getter                         | no     | no          | yes   read-only 参数不应该有 getter                  
| large_tuple                             | no     | no          | yes   tuple 不应该包括太多参数                 
| line_length                             | no     | no          | yes   行长度限制                 
| nesting                                 | no     | no          | yes   类型定义嵌套不要超过1层 , 声明嵌套不要超过5层                
| notification_center_detachment          | no     | no          | yes   `NotificationCenter.default.removeObserver` 只在 `deinit` 中被调用                         
| operator_whitespace                     | no     | no          | yes   定义操作符的时候 操作符左右应该各有一个空格                                 
| private_unit_test                       | no     | no          | yes   单元测试方法 不能设置为 private                             
| redundant_string_enum_value             | no     | no          | yes   字符串类型枚举, 会有默认 string 值,与名字相同, 不要再次设置                               
| shorthand_operator                      | no     | no          | yes   使用 +=, -=, *=, /=                 
| syntactic_sugar                         | no     | no          | yes   要使用 [] ? 等数组字典可选项的语法糖
| todo                                    | no     | no          | yes   避免 TODOs and FIXMEs 标识
| trailing_comma                          | no     | no          | yes   数组末尾不要加括号
| type_body_length                        | no     | no          | yes   类型体行数限制
| type_name                               | no     | no          | yes   类型名字限制规则
| unused_enumerated                       | no     | no          | yes   当参数没有被全部使用的时候, 不要使用容器的 enumerated 方法
| unused_optional_binding                 | no     | no          | yes   必须使用定义的 optional binding
| valid_ibinspectable                     | no     | no          | yes   IBInspectable 必须是可变参数
| vertical_parameter_alignment            | no     | no          | yes   函数参数分为多行书写的时候, 头部(小括号后面一位)必须对其
| weak_delegate                           | no     | no          | yes   delegate 应该被设置为 weak
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容