Discouraged Direct Initialization

Discouraged direct initialization of types that can be harmful

  • Identifier: discouraged_direct_init
  • Enabled by default: Yes
  • Supports autocorrection: No
  • Kind: lint
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning
    types [“Bundle”, “Bundle.init”, “NSError”, “NSError.init”, “UIDevice”, “UIDevice.init”]

Non Triggering Examples

let foo = UIDevice.current
let foo = Bundle.main
let foo = Bundle(path: "bar")
let foo = Bundle(identifier: "bar")
let foo = Bundle.init(path: "bar")
let foo = Bundle.init(identifier: "bar")
let foo = NSError(domain: "bar", code: 0)
let foo = NSError.init(domain: "bar", code: 0)
func testNSError()

Triggering Examples

UIDevice()
Bundle()
let foo = UIDevice()
let foo = Bundle()
let foo = NSError()
let foo = bar(bundle: Bundle(), device: UIDevice(), error: NSError())
UIDevice.init()
Bundle.init()
NSError.init()
let foo = UIDevice.init()
let foo = Bundle.init()
let foo = bar(bundle: Bundle.init(), device: UIDevice.init(), error: NSError.init())