Skip to content

Commit 4d54de3

Browse files
committed
Address naming suggestions and use guard let instead of if let
1 parent fcdf5ee commit 4d54de3

File tree

1 file changed

+45
-54
lines changed

1 file changed

+45
-54
lines changed

Sources/SwiftFormat/Rules/OmitReturns.swift

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,59 +26,56 @@ public final class OmitReturns: SyntaxFormatRule {
2626
let decl = super.visit(node)
2727

2828
// func <name>() -> <Type> { return ... }
29-
if var funcDecl = decl.as(FunctionDeclSyntax.self),
30-
let body = funcDecl.body,
31-
let `return` = containsSingleReturn(body.statements) {
32-
funcDecl.body?.statements = unwrapReturnStmt(`return`)
33-
diagnose(.omitReturnStatement, on: `return`, severity: .refactoring)
34-
return DeclSyntax(funcDecl)
29+
guard var funcDecl = decl.as(FunctionDeclSyntax.self),
30+
let body = funcDecl.body,
31+
let returnStmt = containsSingleReturn(body.statements) else {
32+
return decl
3533
}
3634

37-
return decl
35+
funcDecl.body?.statements = unwrapReturnStmt(returnStmt)
36+
diagnose(.omitReturnStatement, on: returnStmt, severity: .refactoring)
37+
return DeclSyntax(funcDecl)
3838
}
3939

4040
public override func visit(_ node: SubscriptDeclSyntax) -> DeclSyntax {
4141
let decl = super.visit(node)
4242

43-
guard var `subscript` = decl.as(SubscriptDeclSyntax.self) else {
43+
guard var subscriptDecl = decl.as(SubscriptDeclSyntax.self),
44+
let accessorBlock = subscriptDecl.accessorBlock,
45+
// We are assuming valid Swift code here where only
46+
// one `get { ... }` is allowed.
47+
let transformed = transformAccessorBlock(accessorBlock) else {
4448
return decl
4549
}
4650

47-
if let accessorBlock = `subscript`.accessorBlock,
48-
// We are assuming valid Swift code here where only
49-
// one `get { ... }` is allowed.
50-
let transformed = transformAccessorBlock(accessorBlock) {
51-
`subscript`.accessorBlock = transformed
52-
return DeclSyntax(`subscript`)
53-
}
54-
55-
return decl
51+
subscriptDecl.accessorBlock = transformed
52+
return DeclSyntax(subscriptDecl)
5653
}
5754

5855
public override func visit(_ node: PatternBindingSyntax) -> PatternBindingSyntax {
5956
var binding = node
6057

61-
if let accessorBlock = binding.accessorBlock,
62-
let transformed = transformAccessorBlock(accessorBlock) {
63-
binding.accessorBlock = transformed
64-
return binding
58+
guard let accessorBlock = binding.accessorBlock,
59+
let transformed = transformAccessorBlock(accessorBlock) else {
60+
return node
6561
}
6662

67-
return node
63+
binding.accessorBlock = transformed
64+
return binding
6865
}
6966

7067
public override func visit(_ node: ClosureExprSyntax) -> ExprSyntax {
7168
let expr = super.visit(node)
7269

7370
// test { return ... }
74-
if var closure = expr.as(ClosureExprSyntax.self),
75-
let `return` = containsSingleReturn(closure.statements) {
76-
closure.statements = unwrapReturnStmt(`return`)
77-
diagnose(.omitReturnStatement, on: `return`, severity: .refactoring)
78-
return ExprSyntax(closure)
71+
guard var closureExpr = expr.as(ClosureExprSyntax.self),
72+
let returnStmt = containsSingleReturn(closureExpr.statements) else {
73+
return expr
7974
}
8075

81-
return expr
76+
closureExpr.statements = unwrapReturnStmt(returnStmt)
77+
diagnose(.omitReturnStatement, on: returnStmt, severity: .refactoring)
78+
return ExprSyntax(closureExpr)
8279
}
8380

8481
private func transformAccessorBlock(_ accessorBlock: AccessorBlockSyntax) -> AccessorBlockSyntax? {
@@ -93,7 +90,7 @@ public final class OmitReturns: SyntaxFormatRule {
9390
}
9491

9592
guard let body = getter.body,
96-
let `return` = containsSingleReturn(body.statements) else {
93+
let returnStmt = containsSingleReturn(body.statements) else {
9794
return nil
9895
}
9996

@@ -103,50 +100,44 @@ public final class OmitReturns: SyntaxFormatRule {
103100
return nil
104101
}
105102

106-
getter.body?.statements = unwrapReturnStmt(`return`)
103+
getter.body?.statements = unwrapReturnStmt(returnStmt)
107104

108-
diagnose(.omitReturnStatement, on: `return`, severity: .refactoring)
105+
diagnose(.omitReturnStatement, on: returnStmt, severity: .refactoring)
109106

110-
return .init(
111-
leadingTrivia: accessorBlock.leadingTrivia,
112-
leftBrace: accessorBlock.leftBrace,
113-
accessors: .accessors(accessors.with(\.[getterAt], getter)),
114-
rightBrace: accessorBlock.rightBrace,
115-
trailingTrivia: accessorBlock.trailingTrivia)
107+
var newBlock = accessorBlock
108+
newBlock.accessors = .accessors(accessors.with(\.[getterAt], getter))
109+
return newBlock
116110

117111
case .getter(let getter):
118-
guard let `return` = containsSingleReturn(getter) else {
112+
guard let returnStmt = containsSingleReturn(getter) else {
119113
return nil
120114
}
121115

122-
diagnose(.omitReturnStatement, on: `return`, severity: .refactoring)
116+
diagnose(.omitReturnStatement, on: returnStmt, severity: .refactoring)
123117

124-
return .init(
125-
leadingTrivia: accessorBlock.leadingTrivia,
126-
leftBrace: accessorBlock.leftBrace,
127-
accessors: .getter(unwrapReturnStmt(`return`)),
128-
rightBrace: accessorBlock.rightBrace,
129-
trailingTrivia: accessorBlock.trailingTrivia)
118+
var newBlock = accessorBlock
119+
newBlock.accessors = .getter(unwrapReturnStmt(returnStmt))
120+
return newBlock
130121
}
131122
}
132123

133124
private func containsSingleReturn(_ body: CodeBlockItemListSyntax) -> ReturnStmtSyntax? {
134-
if let element = body.firstAndOnly?.as(CodeBlockItemSyntax.self),
135-
let ret = element.item.as(ReturnStmtSyntax.self),
136-
!ret.children(viewMode: .all).isEmpty, ret.expression != nil {
137-
return ret
125+
guard let element = body.firstAndOnly?.as(CodeBlockItemSyntax.self),
126+
let returnStmt = element.item.as(ReturnStmtSyntax.self) else
127+
{
128+
return nil
138129
}
139130

140-
return nil
131+
return !returnStmt.children(viewMode: .all).isEmpty && returnStmt.expression != nil ? returnStmt : nil
141132
}
142133

143-
private func unwrapReturnStmt(_ `return`: ReturnStmtSyntax) -> CodeBlockItemListSyntax {
134+
private func unwrapReturnStmt(_ returnStmt: ReturnStmtSyntax) -> CodeBlockItemListSyntax {
144135
CodeBlockItemListSyntax([
145136
CodeBlockItemSyntax(
146-
leadingTrivia: `return`.leadingTrivia,
147-
item: .expr(`return`.expression!),
137+
leadingTrivia: returnStmt.leadingTrivia,
138+
item: .expr(returnStmt.expression!),
148139
semicolon: nil,
149-
trailingTrivia: `return`.trailingTrivia)
140+
trailingTrivia: returnStmt.trailingTrivia)
150141
])
151142
}
152143
}

0 commit comments

Comments
 (0)