@@ -37,6 +37,69 @@ public final class OmitReturns: SyntaxFormatRule {
37
37
return decl
38
38
}
39
39
40
+ public override func visit( _ node: SubscriptDeclSyntax ) -> DeclSyntax {
41
+ let decl = super. visit ( node)
42
+
43
+ guard var `subscript` = decl. as ( SubscriptDeclSyntax . self) else {
44
+ return decl
45
+ }
46
+
47
+ if let accessorBlock = `subscript`. accessorBlock {
48
+ // We are assuming valid Swift code here where only
49
+ // one `get { ... }` is allowed.
50
+ switch accessorBlock. accessors {
51
+ case . accessors( let accessors) :
52
+ guard var getter = accessors. filter ( {
53
+ $0. accessorSpecifier. tokenKind == . keyword( . get)
54
+ } ) . first else {
55
+ return decl
56
+ }
57
+
58
+ guard let body = getter. body,
59
+ let `return` = containsSingleReturn ( body. statements) else {
60
+ return decl
61
+ }
62
+
63
+ guard let getterAt = accessors. firstIndex ( where: {
64
+ $0. accessorSpecifier. tokenKind == . keyword( . get)
65
+ } ) else {
66
+ return decl
67
+ }
68
+
69
+ getter. body? . statements = unwrapReturnStmt ( `return`)
70
+
71
+ `subscript`. accessorBlock = . init(
72
+ leadingTrivia: accessorBlock. leadingTrivia,
73
+ leftBrace: accessorBlock. leftBrace,
74
+ accessors: . accessors( accessors. with ( \. [ getterAt] , getter) ) ,
75
+ rightBrace: accessorBlock. rightBrace,
76
+ trailingTrivia: accessorBlock. trailingTrivia)
77
+
78
+ diagnose ( . omitReturnStatement, on: `return`, severity: . refactoring)
79
+
80
+ return DeclSyntax ( `subscript`)
81
+
82
+ case . getter( let getter) :
83
+ guard let `return` = containsSingleReturn ( getter) else {
84
+ return decl
85
+ }
86
+
87
+ `subscript`. accessorBlock = . init(
88
+ leadingTrivia: accessorBlock. leadingTrivia,
89
+ leftBrace: accessorBlock. leftBrace,
90
+ accessors: . getter( unwrapReturnStmt ( `return`) ) ,
91
+ rightBrace: accessorBlock. rightBrace,
92
+ trailingTrivia: accessorBlock. trailingTrivia)
93
+
94
+ diagnose ( . omitReturnStatement, on: `return`, severity: . refactoring)
95
+
96
+ return DeclSyntax ( `subscript`)
97
+ }
98
+ }
99
+
100
+ return decl
101
+ }
102
+
40
103
public override func visit( _ node: ClosureExprSyntax ) -> ExprSyntax {
41
104
let expr = super. visit ( node)
42
105
0 commit comments