Opening Brace Spacing

The correct positioning of braces that introduce a block of code or member list is highly controversial. No matter which style is preferred, consistency is key. Apart from different tastes, the positioning of braces can also have a significant impact on the readability of the code, especially for visually impaired developers. This rule ensures that braces are preceded by a single space and on the same line as the declaration. Comments between the declaration and the opening brace are respected. Check out the contrasted_opening_brace rule for a different style.

  • Identifier: opening_brace
  • Enabled by default: Yes
  • Supports autocorrection: Yes
  • Kind: style
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning
    ignore_multiline_type_headers false
    ignore_multiline_statement_conditions false
    ignore_multiline_function_signatures false

Non Triggering Examples

func abc() {
}
[].map() { $0 }
[].map({ })
if let a = b { }
while a == b { }
guard let a = b else { }
struct Rule {}
struct Parent {
    struct Child {
        let foo: Int
    }
}
func f(rect: CGRect) {
    {
        let centre = CGPoint(x: rect.midX, y: rect.midY)
        print(centre)
    }()
}
func f(rect: CGRect) -> () -> Void {
    {
        let centre = CGPoint(x: rect.midX, y: rect.midY)
        print(centre)
    }
}
func f() -> () -> Void {
    {}
}
class Rule:
  NSObject {
  var a: String {
    return ""
  }
}
self.foo(
    (
        "String parameter",
        { "Do something here" }
    )
)
let pattern = #/(\{(?<key>\w+)\})/#
if c {}
else {}
    if c /* comment */ {
        return
    }

Triggering Examples

func abc()↓{
}
func abc()
    ↓{ }
func abc(a: A,
    b: B)
↓{
[].map()↓{ $0 }
struct OldContentView: View {
  @State private var showOptions = false

  var body: some View {
    Button(action: {
      self.showOptions.toggle()
    })↓{
      Image(systemName: "gear")
    }
  }
}
struct OldContentView: View {
  @State private var showOptions = false

  var body: some View {
    Button(action: {
      self.showOptions.toggle()
    })
   ↓{
      Image(systemName: "gear")
    }
  }
}
struct OldContentView: View {
  @State private var showOptions = false

  var body: some View {
    Button {
      self.showOptions.toggle()
    } label:↓{
      Image(systemName: "gear")
    }
  }
}
if let a = b↓{ }
while a == b↓{ }
guard let a = b else↓{ }
if
    let a = b,
    let c = d
    where a == c↓{ }
while
    let a = b,
    let c = d
    where a == c↓{ }
guard
    let a = b,
    let c = d
    where a == c else↓{ }
struct Rule↓{}
struct Rule
↓{
}
struct Rule

    ↓{
}
struct Parent {
    struct Child
    ↓{
        let foo: Int
    }
}
switch a↓{}
if
    let a = b,
    let c = d,
    a == c
↓{ }
while
    let a = b,
    let c = d,
    a == c
↓{ }
guard
    let a = b,
    let c = d,
    a == c else
↓{ }
class Rule↓{}

actor Rule↓{}

enum Rule↓{}

protocol Rule↓{}

extension Rule↓{}

class Rule {
  var a: String↓{
    return ""
  }
}
class Rule {
  var a: String {
    willSet↓{

    }
    didSet  ↓{

    }
  }
}
precedencegroup Group↓{
  assignment: true
}
if
    "test".isEmpty
↓{
    // code here
}
func fooFun() {
    let foo: String? = "foo"
    let bar: String? = "bar"

    if
        let foo = foo,
        let bar = bar
    ↓{
        print(foo + bar)
    }
}
if
    let a = ["A", "B"].first,
    let b = ["B"].first
↓{
    print(a)
}
if c  ↓{}
else /* comment */  ↓{}