File tree Expand file tree Collapse file tree 2 files changed +44
-2
lines changed
Sources/SwiftFormat/Rules
Tests/SwiftFormatTests/Rules Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ public final class OmitExplicitReturns: SyntaxFormatRule {
60
60
guard let accessorBlock = binding. accessorBlock,
61
61
let transformed = transformAccessorBlock ( accessorBlock)
62
62
else {
63
- return node
63
+ return super . visit ( node)
64
64
}
65
65
66
66
binding. accessorBlock = transformed
@@ -145,7 +145,7 @@ public final class OmitExplicitReturns: SyntaxFormatRule {
145
145
CodeBlockItemListSyntax ( [
146
146
CodeBlockItemSyntax (
147
147
leadingTrivia: returnStmt. leadingTrivia,
148
- item: . expr( returnStmt. expression!) ,
148
+ item: . expr( returnStmt. expression!. detached . with ( \ . trailingTrivia , [ ] ) ) ,
149
149
semicolon: nil ,
150
150
trailingTrivia: returnStmt. trailingTrivia
151
151
)
Original file line number Diff line number Diff line change @@ -119,4 +119,46 @@ final class OmitReturnsTests: LintOrFormatRuleTestCase {
119
119
]
120
120
)
121
121
}
122
+
123
+ func testInVariableBindings( ) {
124
+ assertFormatting (
125
+ OmitExplicitReturns . self,
126
+ input: """
127
+ var f = l.filter { 1️⃣return $0.a != o }
128
+ var bar = l.filter {
129
+ 2️⃣return $0.a != o
130
+ }
131
+ """ ,
132
+ expected: """
133
+ var f = l.filter { $0.a != o }
134
+ var bar = l.filter {
135
+ $0.a != o
136
+ }
137
+ """ ,
138
+ findings: [
139
+ FindingSpec ( " 1️⃣ " , message: " 'return' can be omitted because body consists of a single expression " ) ,
140
+ FindingSpec ( " 2️⃣ " , message: " 'return' can be omitted because body consists of a single expression " ) ,
141
+ ]
142
+ )
143
+ }
144
+
145
+ func testInVariableBindingWithTrailingTrivia( ) {
146
+ assertFormatting (
147
+ OmitExplicitReturns . self,
148
+ input: """
149
+ var f = l.filter {
150
+ 1️⃣return $0.a != o // comment
151
+ }
152
+ """ ,
153
+ expected: """
154
+ var f = l.filter {
155
+ $0.a != o // comment
156
+ }
157
+ """ ,
158
+ findings: [
159
+ FindingSpec ( " 1️⃣ " , message: " 'return' can be omitted because body consists of a single expression " )
160
+ ]
161
+ )
162
+ }
163
+
122
164
}
You can’t perform that action at this time.
0 commit comments