Function Default Parameter at End

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
function_default_parameter_at_end idiomatic 3.0.0

最好给函数的最后一个参数赋上默认值

示例

非触发

  1. func foo(baz: String, bar: Int = 0) {}
  2. func foo(x: String, y: Int = 0, z: CGFloat = 0) {}
  3. func foo(bar: String, baz: Int = 0, z: () -> Void) {}
  4. func foo(bar: String, z: () -> Void, baz: Int = 0) {}
  5. func foo(bar: Int = 0) {}
  6. func foo() {}
  7. class A: B {
  8. override func foo(bar: Int = 0, baz: String) {}
  9. func foo(bar: Int = 0, completion: @escaping CompletionHandler) {}
  10. func foo(a: Int, b: CGFloat = 0) {
  11. let block = { (error: Error?) in }
  12. }

触发

  1. func foo(bar: Int = 0, baz: String) {}

Function Parameter Count

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
function_parameter_count metrics 3.0.0

函数的参数应该尽可能的少

示例

非触发

  1. init(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}
  2. init (a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}
  3. `init`(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}
  4. init?(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}
  5. init?<T>(a: T, b: Int, c: Int, d: Int, e: Int, f: Int) {}
  6. init?<T: String>(a: T, b: Int, c: Int, d: Int, e: Int, f: Int) {}
  7. func f2(p1: Int, p2: Int) { }
  8. func f(a: Int, b: Int, c: Int, d: Int, x: Int = 42) {}
  9. func f(a: [Int], b: Int, c: Int, d: Int, f: Int) -> [Int] {
  10. let s = a.flatMap { $0 as? [String: Int] } ?? []}}
  11. override func f(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}

触发

  1. func f(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}
  2. func initialValue(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}
  3. func f(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int = 2, g: Int) {}
  4. struct Foo {
  5. init(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}
  6. func bar(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {}}

Generic Type Name

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
generic_type_name idiomatic 3.0.0

泛型应当只包含字母,首字母大写并且不超过20个字符。

示例

非触发

  1. func foo<T>() {}
  2. func foo<T>() -> T {}
  3. func foo<T, U>(param: U) -> T {}
  4. func foo<T: Hashable, U: Rule>(param: U) -> T {}
  5. struct Foo<T> {}
  6. class Foo<T> {}
  7. enum Foo<T> {}
  8. func run(_ options: NoOptions<CommandantError<()>>) {}
  9. func foo(_ options: Set<type>) {}
  10. func < <T: Comparable>(lhs: T?, rhs: T?) -> Bool
  11. func configureWith(data: Either<MessageThread, (project: Project, backing: Backing)>)
  12. typealias StringDictionary<T> = Dictionary<String, T>
  13. typealias BackwardTriple<T1, T2, T3> = (T3, T2, T1)
  14. typealias DictionaryOfStrings<T : Hashable> = Dictionary<T, String>

触发

  1. func foo<↓T_Foo>() {}
  2. func foo<T, U_Foo>(param: U_Foo) -> T {}
  3. func foo<↓TTTTTTTTTTTTTTTTTTTTT>() {}
  4. func foo<↓type>() {}
  5. typealias StringDictionary<↓T_Foo> = Dictionary<String, T_Foo>
  6. typealias BackwardTriple<T1, T2_Bar, T3> = (T3, T2_Bar, T1)
  7. typealias DictionaryOfStrings<↓T_Foo: Hashable> = Dictionary<T_Foo, String>
  8. class Foo<↓T_Foo> {}
  9. class Foo<T, U_Foo> {}
  10. class Foo<↓T_Foo, U_Foo> {}
  11. class Foo<↓TTTTTTTTTTTTTTTTTTTTT> {}
  12. class Foo<↓type> {}
  13. struct Foo<↓T_Foo> {}
  14. struct Foo<T, U_Foo> {}
  15. struct Foo<↓T_Foo, U_Foo> {}
  16. struct Foo<↓TTTTTTTTTTTTTTTTTTTTT> {}
  17. struct Foo<↓type> {}
  18. enum Foo<↓T_Foo> {}
  19. enum Foo<T, U_Foo> {}
  20. enum Foo<↓T_Foo, U_Foo> {}
  21. enum Foo<↓TTTTTTTTTTTTTTTTTTTTT> {}
  22. enum Foo<↓type> {}

Identical Operands

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
identical_operands lint 3.0.0

比较两个相同的对象是错误的行为

示例

非触发

  1. 1 == 2
  2. foo == bar
  3. prefixedFoo == foo
  4. foo.aProperty == foo.anotherProperty
  5. self.aProperty == self.anotherProperty
  6. "1 == 1"
  7. self.aProperty == aProperty
  8. lhs.aProperty == rhs.aProperty
  9. lhs.identifier == rhs.identifier
  10. i == index
  11. $0 == 0
  12. keyValues?.count ?? 0 == 0
  13. 1 != 2
  14. foo != bar
  15. prefixedFoo != foo
  16. foo.aProperty != foo.anotherProperty
  17. self.aProperty != self.anotherProperty
  18. "1 != 1"
  19. self.aProperty != aProperty
  20. lhs.aProperty != rhs.aProperty
  21. lhs.identifier != rhs.identifier
  22. i != index
  23. $0 != 0
  24. keyValues?.count ?? 0 != 0
  25. 1 === 2
  26. foo === bar
  27. prefixedFoo === foo
  28. foo.aProperty === foo.anotherProperty
  29. self.aProperty === self.anotherProperty
  30. "1 === 1"
  31. self.aProperty === aProperty
  32. lhs.aProperty === rhs.aProperty
  33. lhs.identifier === rhs.identifier
  34. i === index
  35. $0 === 0
  36. keyValues?.count ?? 0 === 0
  37. 1 !== 2
  38. foo !== bar
  39. prefixedFoo !== foo
  40. foo.aProperty !== foo.anotherProperty
  41. self.aProperty !== self.anotherProperty
  42. "1 !== 1"
  43. self.aProperty !== aProperty
  44. lhs.aProperty !== rhs.aProperty
  45. lhs.identifier !== rhs.identifier
  46. i !== index
  47. $0 !== 0
  48. keyValues?.count ?? 0 !== 0
  49. 1 > 2
  50. foo > bar
  51. prefixedFoo > foo
  52. foo.aProperty > foo.anotherProperty
  53. self.aProperty > self.anotherProperty
  54. "1 > 1"
  55. self.aProperty > aProperty
  56. lhs.aProperty > rhs.aProperty
  57. lhs.identifier > rhs.identifier
  58. i > index
  59. $0 > 0
  60. keyValues?.count ?? 0 > 0
  61. 1 >= 2
  62. foo >= bar
  63. prefixedFoo >= foo
  64. foo.aProperty >= foo.anotherProperty
  65. self.aProperty >= self.anotherProperty
  66. "1 >= 1"
  67. self.aProperty >= aProperty
  68. lhs.aProperty >= rhs.aProperty
  69. lhs.identifier >= rhs.identifier
  70. i >= index
  71. $0 >= 0
  72. keyValues?.count ?? 0 >= 0
  73. 1 < 2
  74. foo < bar
  75. prefixedFoo < foo
  76. foo.aProperty < foo.anotherProperty
  77. self.aProperty < self.anotherProperty
  78. "1 < 1"
  79. self.aProperty < aProperty
  80. lhs.aProperty < rhs.aProperty
  81. lhs.identifier < rhs.identifier
  82. i < index
  83. $0 < 0
  84. keyValues?.count ?? 0 < 0
  85. 1 <= 2
  86. foo <= bar
  87. prefixedFoo <= foo
  88. foo.aProperty <= foo.anotherProperty
  89. self.aProperty <= self.anotherProperty
  90. "1 <= 1"
  91. self.aProperty <= aProperty
  92. lhs.aProperty <= rhs.aProperty
  93. lhs.identifier <= rhs.identifier
  94. i <= index
  95. $0 <= 0
  96. keyValues?.count ?? 0 <= 0
  97. func evaluate(_ mode: CommandMode) -> Result<AutoCorrectOptions, CommandantError<CommandantError<()>>>

触发

  1. 1 == 1
  2. foo == foo
  3. foo.aProperty == foo.aProperty
  4. self.aProperty == self.aProperty
  5. $0 == $0
  6. 1 != 1
  7. foo != foo
  8. foo.aProperty != foo.aProperty
  9. self.aProperty != self.aProperty
  10. $0 != $0
  11. 1 === 1
  12. foo === foo
  13. foo.aProperty === foo.aProperty
  14. self.aProperty === self.aProperty
  15. $0 === $0
  16. 1 !== 1
  17. foo !== foo
  18. foo.aProperty !== foo.aProperty
  19. self.aProperty !== self.aProperty
  20. $0 !== $0
  21. 1 > 1
  22. foo > foo
  23. foo.aProperty > foo.aProperty
  24. self.aProperty > self.aProperty
  25. $0 > $0
  26. 1 >= 1
  27. foo >= foo
  28. foo.aProperty >= foo.aProperty
  29. self.aProperty >= self.aProperty
  30. $0 >= $0
  31. 1 < 1
  32. foo < foo
  33. foo.aProperty < foo.aProperty
  34. self.aProperty < self.aProperty
  35. $0 < $0
  36. 1 <= 1
  37. foo <= foo
  38. foo.aProperty <= foo.aProperty
  39. self.aProperty <= self.aProperty
  40. $0 <= $0

Identifier Name

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
identifier_name style 3.0.0

标识符名称应当只包含字母并且以小写字母开头,或者只包含大写字母。当定义static或者let时,可以以大写字母开头。变量名长度不应太长或太短。

示例

非触发

  1. let myLet = 0
  2. var myVar = 0
  3. private let _myLet = 0
  4. class Abc { static let MyLet = 0 }
  5. let URL: NSURL? = nil
  6. let XMLString: String? = nil
  7. override var i = 0
  8. enum Foo { case myEnum }
  9. func isOperator(name: String) -> Bool
  10. func typeForKind(_ kind: SwiftDeclarationKind) -> String
  11. func == (lhs: SyntaxToken, rhs: SyntaxToken) -> Bool
  12. override func IsOperator(name: String) -> Bool
  13. enum Foo { case `private` }
  14. enum Foo { case value(String) }

触发

  1. let MyLet = 0
  2. let _myLet = 0
  3. private let myLet_ = 0
  4. let myExtremelyVeryVeryVeryVeryVeryVeryLongLet = 0
  5. var myExtremelyVeryVeryVeryVeryVeryVeryLongVar = 0
  6. private let _myExtremelyVeryVeryVeryVeryVeryVeryLongLet = 0
  7. let i = 0
  8. var id = 0
  9. private let _i = 0
  10. func IsOperator(name: String) -> Bool
  11. enum Foo { case MyEnum }

Implicit Getter

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
implicit_getter style 3.0.0

算值可读属性和下标语法应该避免使用get关键字

示例

非触发

  1. class Foo {
  2. var foo: Int {
  3. get { return 3 }
  4. set { _abc = newValue }
  5. }
  6. }
  7. class Foo {
  8. var foo: Int {
  9. return 20
  10. }
  11. }
  12. class Foo {
  13. static var foo: Int {
  14. return 20
  15. }
  16. }
  17. class Foo {
  18. static var foo: Int {
  19. get { return 3 }
  20. set { _abc = newValue }
  21. }
  22. }
  23. class Foo {
  24. var foo: Int
  25. }
  26. class Foo {
  27. var foo: Int {
  28. return getValueFromDisk()
  29. }
  30. }
  31. class Foo {
  32. var foo: String {
  33. return "get"
  34. }
  35. }
  36. protocol Foo {
  37. var foo: Int { get }
  38. protocol Foo {
  39. var foo: Int { get set }
  40. class Foo {
  41. var foo: Int {
  42. struct Bar {
  43. var bar: Int {
  44. get { return 1 }
  45. set { _ = newValue }
  46. }
  47. }
  48. return Bar().bar
  49. }
  50. }
  51. var _objCTaggedPointerBits: UInt {
  52. @inline(__always) get { return 0 }
  53. }
  54. var next: Int? {
  55. mutating get {
  56. defer { self.count += 1 }
  57. return self.count
  58. }
  59. }
  60. class Foo {
  61. subscript(i: Int) -> Int {
  62. return 20
  63. }
  64. }
  65. class Foo {
  66. subscript(i: Int) -> Int {
  67. get { return 3 }
  68. set { _abc = newValue }
  69. }
  70. }
  71. protocol Foo {
  72. subscript(i: Int) -> Int { get }
  73. }
  74. protocol Foo {
  75. subscript(i: Int) -> Int { get set }
  76. }

触发

  1. class Foo {
  2. var foo: Int {
  3. get {
  4. return 20
  5. }
  6. }
  7. }
  8. class Foo {
  9. var foo: Int {
  10. get{ return 20 }
  11. }
  12. }
  13. class Foo {
  14. static var foo: Int {
  15. get {
  16. return 20
  17. }
  18. }
  19. }
  20. var foo: Int {
  21. get { return 20 }
  22. }
  23. class Foo {
  24. @objc func bar() {}
  25. var foo: Int {
  26. get {
  27. return 20
  28. }
  29. }
  30. }
  31. class Foo {
  32. subscript(i: Int) -> Int {
  33. get {
  34. return 20
  35. }
  36. }
  37. }

补充

这里并不是说不能使用get关键字,而是说get应当以隐式调用的方式存在,显示调用并不是那么的合理,因为限定了可读。
Properties
Subscripts


Implicit Return

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
implicit_return style 3.0.0

闭包中优先使用隐式返回。

示例

非触发

  1. foo.map { $0 + 1 }
  2. foo.map({ $0 + 1 })
  3. foo.map { value in value + 1 }
  4. func foo() -> Int {
  5. return 0
  6. }
  7. if foo {
  8. return 0
  9. }
  10. var foo: Bool { return true }

触发

  1. foo.map { value in
  2. return value + 1
  3. }
  4. foo.map {
  5. return $0 + 1
  6. }
  7. foo.map({ return $0 + 1})
  8. [1, 2].first(where: {
  9. return true
  10. })

Implicitly Unwrapped Optional

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
implicitly_unwrapped_optional idiomatic 3.0.0

尽可能少的使用隐式解包类型

示例

非触发

  1. @IBOutlet private var label: UILabel!
  2. @IBOutlet var label: UILabel!
  3. @IBOutlet var label: [UILabel!]
  4. if !boolean {}
  5. let int: Int? = 42
  6. let int: Int? = nil

触发

  1. let label: UILabel!
  2. let IBOutlet: UILabel!
  3. let labels: [UILabel!]
  4. var ints: [Int!] = [42, nil, 42]
  5. let label: IBOutlet!
  6. let int: Int! = 42
  7. let int: Int! = nil
  8. var int: Int! = 42
  9. let int: ImplicitlyUnwrappedOptional<Int>
  10. let collection: AnyCollection<Int!>
  11. func foo(int: Int!) {}

补充

隐式解包Optional


Inert Defer

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
inert_defer lint 3.0.0

如果defer写在最后,应该确保无论如何都要执行到它。

示例

非触发

  1. func example3() {
  2. defer { /* deferred code */ }
  3. print("other code")
  4. }
  5. func example4() {
  6. if condition {
  7. defer { /* deferred code */ }
  8. print("other code")
  9. }
  10. }

触发

  1. func example0() {
  2. defer { /* deferred code */ }
  3. }
  4. func example1() {
  5. defer { /* deferred code */ }
  6. // comment
  7. }
  8. func example2() {
  9. if condition {
  10. defer { /* deferred code */ }
  11. // comment
  12. }
  13. }

补充

guard&defer
个人理解:defer类似与try..catch中的final,不同的是defer可以单独来写,比较常见的场景是资源的关闭、释放。但要确保defer可以被执行到,而不是方法提前return。惰性的


Is Disjoint

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
is_disjoint idiomatic 3.0.0

优先使用Set.isDisjoint(with:)而不是Set.intersection(_:).isEmpty

示例

非触发

  1. _ = Set(syntaxKinds).isDisjoint(with: commentAndStringKindsSet)
  2. let isObjc = !objcAttributes.isDisjoint(with: dictionary.enclosedSwiftAttributes)
  3. _ = Set(syntaxKinds).intersection(commentAndStringKindsSet)
  4. _ = !objcAttributes.intersection(dictionary.enclosedSwiftAttributes)

触发

  1. _ = Set(syntaxKinds).↓intersection(commentAndStringKindsSet).isEmpty
  2. let isObjc = !objcAttributes.↓intersection(dictionary.enclosedSwiftAttributes).isEmpty

补充

intersection(_:)
isDisjoint(with:)


Joined Default Parameter

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
joined_default_parameter idiomatic 3.0.0

不鼓励使用默认分隔符

示例

非触发

  1. let foo = bar.joined()
  2. let foo = bar.joined(separator: ",")
  3. let foo = bar.joined(separator: toto)

触发

  1. let foo = bar.joined(↓separator: "")
  2. let foo = bar.filter(toto)
  3. .joined(↓separator: "")
  4. func foo() -> String {
  5. return ["1", "2"].joined(↓separator: "")
  6. }

Large Tuple

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
large_tuple metrics 3.0.0

元组不应该包含过多成员,过多成员的情况应当使用自定义类型。

示例

非触发

  1. let foo: (Int, Int)
  2. let foo: (start: Int, end: Int)
  3. let foo: (Int, (Int, String))
  4. func foo() -> (Int, Int)
  5. func foo() -> (Int, Int) {}
  6. func foo(bar: String) -> (Int, Int)
  7. func foo(bar: String) -> (Int, Int) {}
  8. func foo() throws -> (Int, Int)
  9. func foo() throws -> (Int, Int) {}
  10. let foo: (Int, Int, Int) -> Void
  11. let foo: (Int, Int, Int) throws -> Void
  12. func foo(bar: (Int, String, Float) -> Void)
  13. func foo(bar: (Int, String, Float) throws -> Void)
  14. var completionHandler: ((_ data: Data?, _ resp: URLResponse?, _ e: NSError?) -> Void)!
  15. func getDictionaryAndInt() -> (Dictionary<Int, String>, Int)?
  16. func getGenericTypeAndInt() -> (Type<Int, String, Float>, Int)?

触发

  1. let foo: (Int, Int, Int)
  2. let foo: (start: Int, end: Int, value: String)
  3. let foo: (Int, (Int, Int, Int))
  4. func foo(↓bar: (Int, Int, Int))
  5. func foo() -> ↓(Int, Int, Int)
  6. func foo() -> ↓(Int, Int, Int) {}
  7. func foo(bar: String) -> ↓(Int, Int, Int)
  8. func foo(bar: String) -> ↓(Int, Int, Int) {}
  9. func foo() throws -> ↓(Int, Int, Int)
  10. func foo() throws -> ↓(Int, Int, Int) {}
  11. func foo() throws -> ↓(Int, ↓(String, String, String), Int) {}
  12. func getDictionaryAndInt() -> (Dictionary<Int, ↓(String, String, String)>, Int)?

Leading Whitespace

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
leand_whitespace style 3.0.0

文件不应该包含空行

示例

非触发

  1. //

触发

  1. //

补充

不赞同,有时候空行是为了代码美观,看着舒服,强迫不留空很难受。


Legacy CGGeometry Functions

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
legacy_cggeometry_functions idiomatic 3.0.0

结构体扩展的属性和方法优于遗留的方法

示例

非触发

  1. rect.width
  2. rect.height
  3. rect.minX
  4. rect.midX
  5. rect.maxX
  6. rect.minY
  7. rect.midY
  8. rect.maxY
  9. rect.isNull
  10. rect.isEmpty
  11. rect.isInfinite
  12. rect.standardized
  13. rect.integral
  14. rect.insetBy(dx: 5.0, dy: -7.0)
  15. rect.offsetBy(dx: 5.0, dy: -7.0)
  16. rect1.union(rect2)
  17. rect1.intersect(rect2)
  18. rect1.contains(rect2)
  19. rect.contains(point)
  20. rect1.intersects(rect2)

触发

  1. CGRectGetWidth(rect)
  2. CGRectGetHeight(rect)
  3. CGRectGetMinX(rect)
  4. CGRectGetMidX(rect)
  5. CGRectGetMaxX(rect)
  6. CGRectGetMinY(rect)
  7. CGRectGetMidY(rect)
  8. CGRectGetMaxY(rect)
  9. CGRectIsNull(rect)
  10. CGRectIsEmpty(rect)
  11. CGRectIsInfinite(rect)
  12. CGRectStandardize(rect)
  13. CGRectIntegral(rect)
  14. CGRectInset(rect, 10, 5)
  15. CGRectOffset(rect, -2, 8.3)
  16. CGRectUnion(rect1, rect2)
  17. CGRectIntersection(rect1, rect2)
  18. CGRectContainsRect(rect1, rect2)
  19. CGRectContainsPoint(rect, point)
  20. CGRectIntersectsRect(rect1, rect2)

legacy Constant

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
legacy_constant idiomatic 3.0.0

结构体范围内的常量优于遗留的全局常量

示例

非触发

  1. CGRect.infinite
  2. CGPoint.zero
  3. CGRect.zero
  4. CGSize.zero
  5. NSPoint.zero
  6. NSRect.zero
  7. NSSize.zero
  8. CGRect.null
  9. CGFloat.pi
  10. Float.pi

触发

  1. CGRectInfinite
  2. CGPointZero
  3. CGRectZero
  4. CGSizeZero
  5. NSZeroPoint
  6. NSZeroRect
  7. NSZeroSize
  8. CGRectNull
  9. CGFloat(M_PI)
  10. Float(M_PI)

Legacy Constructor

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
legacy_constructor idiocmatic 3.0.0

Swift的构造方法优于遗留的构造方法

示例

非触发

  1. CGPoint(x: 10, y: 10)
  2. CGPoint(x: xValue, y: yValue)
  3. CGSize(width: 10, height: 10)
  4. CGSize(width: aWidth, height: aHeight)
  5. CGRect(x: 0, y: 0, width: 10, height: 10)
  6. CGRect(x: xVal, y: yVal, width: aWidth, height: aHeight)
  7. CGVector(dx: 10, dy: 10)
  8. CGVector(dx: deltaX, dy: deltaY)
  9. NSPoint(x: 10, y: 10)
  10. NSPoint(x: xValue, y: yValue)
  11. NSSize(width: 10, height: 10)
  12. NSSize(width: aWidth, height: aHeight)
  13. NSRect(x: 0, y: 0, width: 10, height: 10)
  14. NSRect(x: xVal, y: yVal, width: aWidth, height: aHeight)
  15. NSRange(location: 10, length: 1)
  16. NSRange(location: loc, length: len)
  17. UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 10)
  18. UIEdgeInsets(top: aTop, left: aLeft, bottom: aBottom, right: aRight)
  19. NSEdgeInsets(top: 0, left: 0, bottom: 10, right: 10)
  20. NSEdgeInsets(top: aTop, left: aLeft, bottom: aBottom, right: aRight)
  21. UIOffset(horizontal: 0, vertical: 10)
  22. UIOffset(horizontal: horizontal, vertical: vertical)

触发

  1. CGPointMake(10, 10)
  2. CGPointMake(xVal, yVal)
  3. CGPointMake(calculateX(), 10)
  4. CGSizeMake(10, 10)
  5. CGSizeMake(aWidth, aHeight)
  6. CGRectMake(0, 0, 10, 10)
  7. CGRectMake(xVal, yVal, width, height)
  8. CGVectorMake(10, 10)
  9. CGVectorMake(deltaX, deltaY)
  10. NSMakePoint(10, 10)
  11. NSMakePoint(xVal, yVal)
  12. NSMakeSize(10, 10)
  13. NSMakeSize(aWidth, aHeight)
  14. NSMakeRect(0, 0, 10, 10)
  15. NSMakeRect(xVal, yVal, width, height)
  16. NSMakeRange(10, 1)
  17. NSMakeRange(loc, len)
  18. UIEdgeInsetsMake(0, 0, 10, 10)
  19. UIEdgeInsetsMake(top, left, bottom, right)
  20. NSEdgeInsetsMake(0, 0, 10, 10)
  21. NSEdgeInsetsMake(top, left, bottom, right)
  22. CGVectorMake(10, 10)
  23. NSMakeRange(10, 1)
  24. UIOffsetMake(0, 10)
  25. UIOffsetMake(horizontal, vertical)

Legacy NSGeometry Functions

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
legacy_nsgeometry idiomatic 3.0.0

结构体扩展的属性和方法优于遗留的方法

示例

非触发

  1. rect.width
  2. rect.height
  3. rect.minX
  4. rect.midX
  5. rect.maxX
  6. rect.minY
  7. rect.midY
  8. rect.maxY
  9. rect.isEmpty
  10. rect.integral
  11. rect.insetBy(dx: 5.0, dy: -7.0)
  12. rect.offsetBy(dx: 5.0, dy: -7.0)
  13. rect1.union(rect2)
  14. rect1.intersect(rect2)
  15. rect1.contains(rect2)
  16. rect.contains(point)
  17. rect1.intersects(rect2)

触发

  1. NSWidth(rect)
  2. NSHeight(rect)
  3. NSMinX(rect)
  4. NSMidX(rect)
  5. NSMaxX(rect)
  6. NSMinY(rect)
  7. NSMidY(rect)
  8. NSMaxY(rect)
  9. NSEqualRects(rect1, rect2)
  10. NSEqualSizes(size1, size2)
  11. NSEqualPoints(point1, point2)
  12. NSEdgeInsetsEqual(insets2, insets2)
  13. NSIsEmptyRect(rect)
  14. NSIntegralRect(rect)
  15. NSInsetRect(rect, 10, 5)
  16. NSOffsetRect(rect, -2, 8.3)
  17. NSUnionRect(rect1, rect2)
  18. NSIntersectionRect(rect1, rect2)
  19. NSContainsRect(rect1, rect2)
  20. NSPointInRect(rect, point)
  21. NSIntersectsRect(rect1, rect2)

Legacy Random

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
legacy_random Disabled idiomatic 4.2.0

优先使用type.random(in:)而不是遗留的方法

示例

非触发

  1. Int.random(in: 0..<10)
  2. Double.random(in: 8.6...111.34)
  3. Float.random(in: 0 ..< 1)

触发

  1. arc4random(10)
  2. arc4random_uniform(83)
  3. drand48(52)

Variable Declaration Whitespace

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
let_var_whitespace style 3.0.0

letvar关键字声明应该和其他语句用空白行分开

示例

非触发

  1. let a = 0
  2. var x = 1
  3. x = 2
  4. a = 5
  5. var x = 1
  6. struct X {
  7. var a = 0
  8. }
  9. let a = 1 +
  10. 2
  11. let b = 5
  12. var x: Int {
  13. return 0
  14. }
  15. var x: Int {
  16. let a = 0
  17. return a
  18. }
  19. #if os(macOS)
  20. let a = 0
  21. #endif
  22. @available(swift 4)
  23. let a = 0
  24. class C {
  25. @objc
  26. var s: String = ""
  27. }
  28. class C {
  29. @objc
  30. func a() {}
  31. }
  32. class C {
  33. var x = 0
  34. lazy
  35. var y = 0
  36. }
  37. @available(OSX, introduced: 10.6)
  38. @available(*, deprecated)
  39. var x = 0
  40. // swiftlint:disable superfluous_disable_command
  41. // swiftlint:disable force_cast
  42. let x = bar as! Bar
  43. var x: Int {
  44. let a = 0
  45. return a
  46. }

触发

  1. var x = 1
  2. x = 2
  3. a = 5
  4. var x = 1
  5. struct X {
  6. let a
  7. func x() {}
  8. }
  9. var x = 0
  10. @objc func f() {}
  11. var x = 0
  12. @objc
  13. func f() {}
  14. @objc func f() {
  15. }
  16. var x = 0

Line Length

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
line_length metrics 3.0.0

一行不应包含过多的字符

示例

非触发

  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)
  3. #imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")

触发

  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)
  3. #imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")#imageLiteral(resourceName: "image.jpg")

补充

举例存疑 貌似例子都是一样的。。。
查看源码 可知默认配置是一行不要超过120个字符


Literal Expression End Indentation

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
literal_expression_end_indentation style 3.0.0

ArrayDictionary的开头和末尾应当有相同的缩进

示例

非触发

  1. [1, 2, 3]
  2. [1,
  3. 2
  4. ]
  5. [
  6. 1,
  7. 2
  8. ]
  9. [
  10. 1,
  11. 2]
  12. let x = [
  13. 1,
  14. 2
  15. ]
  16. [key: 2, key2: 3]
  17. [key: 1,
  18. key2: 2
  19. ]
  20. [
  21. key: 0,
  22. key2: 20
  23. ]

触发

  1. let x = [
  2. 1,
  3. 2
  4. ↓]
  5. let x = [
  6. 1,
  7. 2
  8. ↓]
  9. let x = [
  10. key: value
  11. ↓]

Lower ACL than parent

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
lower_acl_than_parent lint 3.0.0

确保定义的(属性、方法)的访问控制权限小于包裹它的(类、结构体)访问控制权限

示例

非触发

  1. public struct Foo { public func bar() {} }
  2. internal struct Foo { func bar() {} }
  3. struct Foo { func bar() {} }
  4. open class Foo { public func bar() {} }
  5. open class Foo { open func bar() {} }
  6. fileprivate struct Foo { private func bar() {} }
  7. private struct Foo { private func bar(id: String) }
  8. extension Foo { public func bar() {} }
  9. private struct Foo { fileprivate func bar() {} }
  10. private func foo(id: String) {}

触发

  1. struct Foo { public func bar() {} }
  2. enum Foo { public func bar() {} }
  3. public class Foo { open func bar() }
  4. class Foo { public private(set) var bar: String? }

Mark

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
mark lint 3.0.0

MARK 标记应当使用正确的格式 比如:// MARK: ...// MARK: -...

示例

非触发

  1. // MARK: good
  2. // MARK: - good
  3. // MARK: -
  4. // BOOKMARK
  5. //BOOKMARK
  6. // BOOKMARKS

触发

  1. //MARK: bad
  2. // MARK:bad
  3. //MARK:bad
  4. // MARK: bad
  5. // MARK: bad
  6. // MARK: -bad
  7. // MARK:- bad
  8. // MARK:-bad
  9. //MARK: - bad
  10. //MARK:- bad
  11. //MARK: -bad
  12. //MARK:-bad
  13. //Mark: bad
  14. // Mark: bad
  15. // MARK bad
  16. //MARK bad
  17. // MARK - bad
  18. //MARK : bad
  19. // MARKL:
  20. // MARKR
  21. // MARKK -
  22. //MARK:- Top-Level bad mark
  23. //MARK:- Another bad mark
  24. struct MarkTest {}
  25. // MARK:- Bad mark
  26. extension MarkTest {}

Missing Docs

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
missing_docs lint 4.1.0

声明必须有文档

示例

非触发

  1. /// docs
  2. public class A {
  3. /// docs
  4. public func b() {}
  5. }
  6. /// docs
  7. public class B: A { override public func b() {} }
  8. import Foundation
  9. /// docs
  10. public class B: NSObject {
  11. // no docs
  12. override public var description: String { fatalError() } }

触发

  1. public func a() {}
  2. // regular comment
  3. public func a() {}
  4. /* regular comment */
  5. public func a() {}
  6. /// docs
  7. public protocol A {
  8. // no docs
  9. var b: Int { get } }
  10. /// docs
  11. public struct C: A {
  12. public let b: Int
  13. }

Modifier Order

标识符 默认开启 支持自动更正 类别 分析仪 最低swift编译版本
modifier_order style 4.1.0

修饰符的顺序应该是一致的

示例

非触发

  1. public class Foo {
  2. public convenience required init() {}
  3. }
  4. public class Foo {
  5. public static let bar = 42
  6. }
  7. public class Foo {
  8. public static var bar: Int {
  9. return 42 }}
  10. public class Foo {
  11. public class var bar: Int {
  12. return 42
  13. }
  14. }
  15. public class Bar {
  16. public class var foo: String {
  17. return "foo"
  18. }
  19. }
  20. public class Foo: Bar {
  21. override public final class var foo: String {
  22. return "bar"
  23. }
  24. }
  25. open class Bar {
  26. public var foo: Int? {
  27. return 42
  28. }
  29. }
  30. open class Foo: Bar {
  31. override public var foo: Int? {
  32. return 43
  33. }
  34. }
  35. open class Bar {
  36. open class func foo() -> Int {
  37. return 42
  38. }
  39. }
  40. class Foo: Bar {
  41. override open class func foo() -> Int {
  42. return 43
  43. }
  44. }
  45. protocol Foo: class {}
  46. class Bar {
  47. public private(set) weak var foo: Foo?
  48. }
  49. @objc
  50. public final class Foo: NSObject {}
  51. @objcMembers
  52. public final class Foo: NSObject {}
  53. @objc
  54. override public private(set) weak var foo: Bar?
  55. @objc
  56. public final class Foo: NSObject {}
  57. @objc
  58. open final class Foo: NSObject {
  59. open weak var weakBar: NSString? = nil
  60. }
  61. public final class Foo {}
  62. class Bar {
  63. func bar() {}
  64. }
  65. internal class Foo: Bar {
  66. override internal func bar() {}
  67. }
  68. public struct Foo {
  69. internal weak var weakBar: NSObject? = nil
  70. }
  71. class Foo {
  72. internal lazy var bar: String = "foo"
  73. }

触发

  1. class Foo {
  2. convenience required public init() {}
  3. }
  4. public class Foo {
  5. static public let bar = 42
  6. }
  7. public class Foo {
  8. static public var bar: Int {
  9. return 42
  10. }
  11. }
  12. public class Foo {
  13. class public var bar: Int {
  14. return 42
  15. }
  16. }
  17. public class RootFoo {
  18. class public var foo: String {
  19. return "foo"
  20. }
  21. }
  22. public class Foo: RootFoo {
  23. override final class public var foo: String {
  24. return "bar"
  25. }
  26. }
  27. open class Bar {
  28. public var foo: Int? {
  29. return 42
  30. }
  31. }
  32. open class Foo: Bar {
  33. public override var foo: Int? {
  34. return 43
  35. }
  36. }
  37. protocol Foo: class {}
  38. class Bar {
  39. private(set) public weak var foo: Foo?
  40. }
  41. open class Bar {
  42. open class func foo() -> Int {
  43. return 42
  44. }
  45. }
  46. class Foo: Bar {
  47. class open override func foo() -> Int {
  48. return 43
  49. }
  50. }
  51. open class Bar {
  52. open class func foo() -> Int {
  53. return 42
  54. }
  55. }
  56. class Foo: Bar {
  57. open override class func foo() -> Int {
  58. return 43
  59. }
  60. }
  61. @objc
  62. final public class Foo: NSObject {}
  63. @objcMembers
  64. final public class Foo: NSObject {}
  65. @objc
  66. final open class Foo: NSObject {
  67. weak open var weakBar: NSString? = nil
  68. }
  69. final public class Foo {}
  70. internal class Foo: Bar {
  71. internal override func bar() {}
  72. }
  73. public struct Foo {
  74. weak internal var weakBar: NSObjetc? = nil
  75. }
  76. class Foo {
  77. lazy internal var bar: String = "foo"
  78. }