Contrasted Opening Brace
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 on a separate line after the declaration to contrast the code block from the rest of the declaration. Comments between the declaration and the opening brace are respected. Check out the opening_brace
rule for a different style.
- Identifier:
contrasted_opening_brace
- Enabled by default: No
- Supports autocorrection: Yes
- Kind: style
- Analyzer rule: No
- Minimum Swift compiler version: 5.0.0
- Default configuration:
Key Value severity warning
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
{
{}
}
@MyProperty 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
}
if c1
{
return
} else if c2
{
return
} else if c3
{
return
}
let a = f.map
{ a in
a
}
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")
} label: ↓{
Image(systemName: "gear")
}
}
}
class Rule
{
var a: String↓{
return ""
}
}
@MyProperty class Rule
{
var a: String
{
willSet↓{
}
didSet ↓{
}
}
}
precedencegroup Group ↓{
assignment: true
}
if
"test".isEmpty ↓{
// code here
}
if c ↓{}
else /* comment */ ↓{}
if c
↓{
// code here
}
if c1 ↓{
return
} else if c2↓{
return
} else if c3
↓{
return
}
func f()
{
return a.map
↓{ $0 }
}
a ↓{
$0
} b: ↓{
$1
}