Skip to content

Commit 66ae806

Browse files
authored
Enable CI format check (#253)
* Incorporate swift-format formatting fixes * Resolve linting issues * Doc edits, mostly to fix linter-identified issues * Early exit improvements * Indentation fixes * Line lengths to 80 maximum * Implement case binding rule, plus argument line break improvements * Implement NoForceUnwrap rule * Format the tests, as well * Stop masquerading as XCTest
1 parent f970eb6 commit 66ae806

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2475
-1964
lines changed

.github/workflows/pull_request.yml

+14-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@ on:
55
types: [opened, reopened, synchronize]
66

77
jobs:
8+
validate_format_config:
9+
name: Validate Format Config
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repo
13+
uses: actions/checkout@v4
14+
- name: Install apt dependencies
15+
run: sudo apt-get -qq update && sudo apt-get -qq -y install curl
16+
- name: Compare against swift-mmio swift-format config
17+
run: |
18+
curl -sL https://raw.githubusercontent.com/apple/swift-mmio/refs/heads/main/.swift-format -o .swift-format-mmio
19+
diff .swift-format .swift-format-mmio
20+
821
tests:
922
name: Test
1023
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
24+
1125
soundness:
1226
name: Soundness
1327
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
@@ -16,5 +30,3 @@ jobs:
1630
license_header_check_enabled: false
1731
# https://github.com/apple/swift-algorithms/issues/251
1832
docs_check_enabled: false
19-
# https://github.com/apple/swift-algorithms/issues/252
20-
format_check_enabled: false

.swift-format

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"fileScopedDeclarationPrivacy" : {
3+
"accessLevel" : "private"
4+
},
5+
"indentation" : {
6+
"spaces" : 2
7+
},
8+
"indentConditionalCompilationBlocks" : false,
9+
"indentSwitchCaseLabels" : false,
10+
"lineBreakAroundMultilineExpressionChainComponents" : false,
11+
"lineBreakBeforeControlFlowKeywords" : false,
12+
"lineBreakBeforeEachArgument" : false,
13+
"lineBreakBeforeEachGenericRequirement" : false,
14+
"lineLength" : 80,
15+
"maximumBlankLines" : 1,
16+
"multiElementCollectionTrailingCommas" : true,
17+
"noAssignmentInExpressions" : {
18+
"allowedFunctions" : [
19+
"XCTAssertNoThrow"
20+
]
21+
},
22+
"prioritizeKeepingFunctionOutputTogether" : false,
23+
"respectsExistingLineBreaks" : true,
24+
"rules" : {
25+
"AllPublicDeclarationsHaveDocumentation" : false,
26+
"AlwaysUseLiteralForEmptyCollectionInit" : true,
27+
"AlwaysUseLowerCamelCase" : true,
28+
"AmbiguousTrailingClosureOverload" : false,
29+
"BeginDocumentationCommentWithOneLineSummary" : true,
30+
"DoNotUseSemicolons" : true,
31+
"DontRepeatTypeInStaticProperties" : true,
32+
"FileScopedDeclarationPrivacy" : true,
33+
"FullyIndirectEnum" : true,
34+
"GroupNumericLiterals" : true,
35+
"IdentifiersMustBeASCII" : true,
36+
"NeverForceUnwrap" : true,
37+
"NeverUseForceTry" : true,
38+
"NeverUseImplicitlyUnwrappedOptionals" : true,
39+
"NoAccessLevelOnExtensionDeclaration" : true,
40+
"NoAssignmentInExpressions" : true,
41+
"NoBlockComments" : true,
42+
"NoCasesWithOnlyFallthrough" : true,
43+
"NoEmptyTrailingClosureParentheses" : true,
44+
"NoLabelsInCasePatterns" : true,
45+
"NoLeadingUnderscores" : false,
46+
"NoParensAroundConditions" : true,
47+
"NoPlaygroundLiterals" : true,
48+
"NoVoidReturnOnFunctionSignature" : true,
49+
"OmitExplicitReturns" : true,
50+
"OneCasePerLine" : true,
51+
"OneVariableDeclarationPerLine" : true,
52+
"OnlyOneTrailingClosureArgument" : true,
53+
"OrderedImports" : true,
54+
"ReplaceForEachWithForLoop" : true,
55+
"ReturnVoidInsteadOfEmptyTuple" : true,
56+
"TypeNamesShouldBeCapitalized" : true,
57+
"UseEarlyExits" : false,
58+
"UseExplicitNilCheckInConditions" : true,
59+
"UseLetInEveryBoundCaseVariable" : true,
60+
"UseShorthandTypeNames" : true,
61+
"UseSingleLinePropertyGetter" : true,
62+
"UseSynthesizedInitializer" : true,
63+
"UseTripleSlashForDocumentationComments" : true,
64+
"UseWhereClausesInForLoops" : false,
65+
"ValidateDocumentationComments" : true
66+
},
67+
"spacesBeforeEndOfLineComments": 2,
68+
"spacesAroundRangeFormationOperators" : false,
69+
"tabWidth" : 2,
70+
"version" : 1
71+
}

Package.swift

+19-19
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
import PackageDescription
1414

1515
let package = Package(
16-
name: "swift-algorithms",
17-
products: [
18-
.library(
19-
name: "Algorithms",
20-
targets: ["Algorithms"]),
21-
],
22-
dependencies: [
23-
.package(url: "https://github.com/apple/swift-numerics.git", from: "1.0.0"),
24-
],
25-
targets: [
26-
.target(
27-
name: "Algorithms",
28-
dependencies: [
29-
.product(name: "RealModule", package: "swift-numerics"),
30-
]),
31-
.testTarget(
32-
name: "SwiftAlgorithmsTests",
33-
dependencies: ["Algorithms"]),
34-
]
16+
name: "swift-algorithms",
17+
products: [
18+
.library(
19+
name: "Algorithms",
20+
targets: ["Algorithms"])
21+
],
22+
dependencies: [
23+
.package(url: "https://github.com/apple/swift-numerics.git", from: "1.0.0")
24+
],
25+
targets: [
26+
.target(
27+
name: "Algorithms",
28+
dependencies: [
29+
.product(name: "RealModule", package: "swift-numerics")
30+
]),
31+
.testTarget(
32+
name: "SwiftAlgorithmsTests",
33+
dependencies: ["Algorithms"]),
34+
]
3535
)

Sources/Algorithms/AdjacentPairs.swift

+39-25
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ extension AdjacentPairsSequence: Sequence {
126126
}
127127

128128
extension AdjacentPairsSequence: LazySequenceProtocol
129-
where Base: LazySequenceProtocol {}
129+
where Base: LazySequenceProtocol {}
130130

131131
/// A collection of adjacent pairs of elements built from an underlying
132132
/// collection.
@@ -143,7 +143,8 @@ public struct AdjacentPairsCollection<Base: Collection> {
143143
@inlinable
144144
internal init(base: Base) {
145145
self.base = base
146-
self.secondBaseIndex = base.isEmpty
146+
self.secondBaseIndex =
147+
base.isEmpty
147148
? base.endIndex
148149
: base.index(after: base.startIndex)
149150
}
@@ -154,7 +155,7 @@ extension AdjacentPairsCollection {
154155
public struct Index: Comparable {
155156
@usableFromInline
156157
internal var first: Base.Index
157-
158+
158159
@usableFromInline
159160
internal var second: Base.Index
160161

@@ -168,7 +169,7 @@ extension AdjacentPairsCollection {
168169
public static func == (lhs: Index, rhs: Index) -> Bool {
169170
lhs.first == rhs.first
170171
}
171-
172+
172173
@inlinable
173174
public static func < (lhs: Index, rhs: Index) -> Bool {
174175
lhs.first < rhs.first
@@ -183,7 +184,7 @@ extension AdjacentPairsCollection: Collection {
183184
first: secondBaseIndex == base.endIndex ? base.endIndex : base.startIndex,
184185
second: secondBaseIndex)
185186
}
186-
187+
187188
@inlinable
188189
public var endIndex: Index {
189190
Index(first: base.endIndex, second: base.endIndex)
@@ -207,20 +208,23 @@ extension AdjacentPairsCollection: Collection {
207208
public func index(_ i: Index, offsetBy distance: Int) -> Index {
208209
guard distance != 0 else { return i }
209210

210-
guard let result = distance > 0
211-
? offsetForward(i, by: distance, limitedBy: endIndex)
212-
: offsetBackward(i, by: -distance, limitedBy: startIndex)
211+
guard
212+
let result = distance > 0
213+
? offsetForward(i, by: distance, limitedBy: endIndex)
214+
: offsetBackward(i, by: -distance, limitedBy: startIndex)
213215
else { fatalError("Index out of bounds") }
214216
return result
215217
}
216218

217219
@inlinable
218220
public func index(
219-
_ i: Index, offsetBy distance: Int, limitedBy limit: Index
221+
_ i: Index,
222+
offsetBy distance: Int,
223+
limitedBy limit: Index
220224
) -> Index? {
221225
guard distance != 0 else { return i }
222226
guard limit != i else { return nil }
223-
227+
224228
if distance > 0 {
225229
let limit = limit > i ? limit : endIndex
226230
return offsetForward(i, by: distance, limitedBy: limit)
@@ -229,38 +233,48 @@ extension AdjacentPairsCollection: Collection {
229233
return offsetBackward(i, by: -distance, limitedBy: limit)
230234
}
231235
}
232-
236+
233237
@inlinable
234238
internal func offsetForward(
235-
_ i: Index, by distance: Int, limitedBy limit: Index
239+
_ i: Index,
240+
by distance: Int,
241+
limitedBy limit: Index
236242
) -> Index? {
237243
assert(distance > 0)
238244
assert(limit > i)
239-
240-
guard let newFirst = base.index(i.second, offsetBy: distance - 1, limitedBy: limit.first),
241-
newFirst != base.endIndex
245+
246+
let newFirst = base.index(
247+
i.second,
248+
offsetBy: distance - 1,
249+
limitedBy: limit.first)
250+
guard let newFirst, newFirst != base.endIndex
242251
else { return nil }
252+
243253
let newSecond = base.index(after: newFirst)
244-
254+
245255
precondition(newSecond <= base.endIndex, "Can't advance beyond endIndex")
246256
return newSecond == base.endIndex
247257
? endIndex
248258
: Index(first: newFirst, second: newSecond)
249259
}
250-
260+
251261
@inlinable
252262
internal func offsetBackward(
253-
_ i: Index, by distance: Int, limitedBy limit: Index
263+
_ i: Index,
264+
by distance: Int,
265+
limitedBy limit: Index
254266
) -> Index? {
255267
assert(distance > 0)
256268
assert(limit < i)
257-
269+
258270
let offset = i == endIndex ? 0 : 1
259-
guard let newSecond = base.index(
271+
let newSecond = base.index(
260272
i.first,
261273
offsetBy: -(distance - offset),
262274
limitedBy: limit.second)
275+
guard let newSecond
263276
else { return nil }
277+
264278
let newFirst = base.index(newSecond, offsetBy: -1)
265279
precondition(newFirst >= base.startIndex, "Can't move before startIndex")
266280
return Index(first: newFirst, second: newSecond)
@@ -282,12 +296,12 @@ extension AdjacentPairsCollection: Collection {
282296
}
283297

284298
extension AdjacentPairsCollection: BidirectionalCollection
285-
where Base: BidirectionalCollection
286-
{
299+
where Base: BidirectionalCollection {
287300
@inlinable
288301
public func index(before i: Index) -> Index {
289302
precondition(i != startIndex, "Can't offset before startIndex")
290-
let second = i == endIndex
303+
let second =
304+
i == endIndex
291305
? base.index(before: base.endIndex)
292306
: i.first
293307
let first = base.index(before: second)
@@ -296,10 +310,10 @@ extension AdjacentPairsCollection: BidirectionalCollection
296310
}
297311

298312
extension AdjacentPairsCollection: RandomAccessCollection
299-
where Base: RandomAccessCollection {}
313+
where Base: RandomAccessCollection {}
300314

301315
extension AdjacentPairsCollection: LazySequenceProtocol, LazyCollectionProtocol
302-
where Base: LazySequenceProtocol {}
316+
where Base: LazySequenceProtocol {}
303317

304318
extension AdjacentPairsCollection.Index: Hashable where Base.Index: Hashable {
305319
@inlinable

0 commit comments

Comments
 (0)