Implicit Return
Prefer implicit returns in closures, functions and getters
- Identifier:
implicit_return
- 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 included [closure, function, getter, initializer, subscript]
Non Triggering Examples
foo.map { $0 + 1 }
foo.map({ $0 + 1 })
foo.map { value in value + 1 }
[1, 2].first(where: {
true
})
func foo() -> Int {
0
}
class Foo {
func foo() -> Int { 0 }
}
func fetch() -> Data? {
do {
return try loadData()
} catch {
return nil
}
}
func f() -> Int {
let i = 4
return i
}
func f() -> Int {
return 3
let i = 2
}
func f() -> Int {
return g()
func g() -> Int { 4 }
}
var foo: Bool { true }
class Foo {
var bar: Int {
get {
0
}
}
}
class Foo {
static var bar: Int {
0
}
}
class C {
let i: Int
init(i: Int) {
if i < 3 {
self.i = 1
return
}
self.i = 2
}
}
class C {
init?() {
let i = 1
return nil
}
}
class C {
subscript(i: Int) -> Int {
let res = i
return res
}
}
Triggering Examples
foo.map { value in
↓return value + 1
}
foo.map {
↓return $0 + 1
}
foo.map({ ↓return $0 + 1})
[1, 2].first(where: {
↓return true
})
func foo() -> Int {
↓return 0
}
class Foo {
func foo() -> Int { ↓return 0 }
}
func f() { ↓return }
var foo: Bool { ↓return true }
class Foo {
var bar: Int {
get {
↓return 0
}
}
}
class Foo {
static var bar: Int {
↓return 0
}
}
class C {
init() {
↓return
}
}
class C {
init?() {
↓return nil
}
}
class C {
subscript(i: Int) -> Int {
↓return i
}
}