Convenience Type

Types used for hosting only static members should be implemented as a caseless enum to avoid instantiation

  • Identifier: convenience_type
  • Enabled by default: No
  • Supports autocorrection: No
  • Kind: idiomatic
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning

Non Triggering Examples

enum Math { // enum
  public static let pi = 3.14
}
// class with inheritance
class MathViewController: UIViewController {
  public static let pi = 3.14
}
@objc class Math: NSObject { // class visible to Obj-C
  public static let pi = 3.14
}
struct Math { // type with non-static declarations
  public static let pi = 3.14
  public let randomNumber = 2
}
class DummyClass {}
class Foo: NSObject { // class with Obj-C class property
    class @objc let foo = 1
}
class Foo: NSObject { // class with Obj-C static property
    static @objc let foo = 1
}
class Foo { // @objc class func can't exist on an enum
   @objc class func foo() {}
}
class Foo { // @objc static func can't exist on an enum
   @objc static func foo() {}
}
@objcMembers class Foo { // @objc static func can't exist on an enum
   static func foo() {}
}
final class Foo { // final class, but @objc class func can't exist on an enum
   @objc class func foo() {}
}
final class Foo { // final class, but @objc static func can't exist on an enum
   @objc static func foo() {}
}
@globalActor actor MyActor {
  static let shared = MyActor()
}

Triggering Examples

struct Math {
  public static let pi = 3.14
}
struct Math {
  public static let pi = 3.14
  @available(*, unavailable) init() {}
}
final class Foo { // final class can't be inherited
    class let foo = 1
}
class Foo {
    class let foo = 1
}
class Foo {
    final class let foo = 1
}
class SomeClass {
    static func foo() {}
}