Skip to content

Commit 99f2bb3

Browse files
authored
Merge pull request #2364 from ahoppen/no-optional-map
Don’t use `Optional.map` or `Optional.flatMap`
2 parents 4391b22 + f9f13a4 commit 99f2bb3

File tree

13 files changed

+90
-33
lines changed

13 files changed

+90
-33
lines changed

Sources/BuildServerIntegration/SwiftPMBuildServer.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,19 +215,29 @@ package actor SwiftPMBuildServer: BuiltInBuildServer {
215215
let hostSDK = try SwiftSDK.hostSwiftSDK(AbsolutePath(validating: destinationToolchainBinDir.filePath))
216216
let hostSwiftPMToolchain = try UserToolchain(swiftSDK: hostSDK)
217217

218+
let triple: Triple? =
219+
if let triple = options.swiftPMOrDefault.triple {
220+
try Triple(triple)
221+
} else {
222+
nil
223+
}
224+
let swiftSDKsDirectory: AbsolutePath? =
225+
if let swiftSDKsDirectory = options.swiftPMOrDefault.swiftSDKsDirectory {
226+
try AbsolutePath(validating: swiftSDKsDirectory, relativeTo: absProjectRoot)
227+
} else {
228+
nil
229+
}
218230
let destinationSDK = try SwiftSDK.deriveTargetSwiftSDK(
219231
hostSwiftSDK: hostSDK,
220232
hostTriple: hostSwiftPMToolchain.targetTriple,
221233
customToolsets: options.swiftPMOrDefault.toolsets?.map {
222234
try AbsolutePath(validating: $0, relativeTo: absProjectRoot)
223235
} ?? [],
224-
customCompileTriple: options.swiftPMOrDefault.triple.map { try Triple($0) },
236+
customCompileTriple: triple,
225237
swiftSDKSelector: options.swiftPMOrDefault.swiftSDK,
226238
store: SwiftSDKBundleStore(
227239
swiftSDKsDirectory: localFileSystem.getSharedSwiftSDKsDirectory(
228-
explicitDirectory: options.swiftPMOrDefault.swiftSDKsDirectory.map {
229-
try AbsolutePath(validating: $0, relativeTo: absProjectRoot)
230-
}
240+
explicitDirectory: swiftSDKsDirectory
231241
),
232242
hostToolchainBinDir: hostSwiftPMToolchain.swiftCompilerPath.parentDirectory,
233243
fileSystem: localFileSystem,
@@ -320,9 +330,13 @@ package actor SwiftPMBuildServer: BuiltInBuildServer {
320330
disableSandbox: options.swiftPMOrDefault.disableSandbox ?? false
321331
)
322332

323-
self.traitConfiguration = TraitConfiguration(
324-
enabledTraits: options.swiftPMOrDefault.traits.flatMap(Set.init)
325-
)
333+
let enabledTraits: Set<String>? =
334+
if let traits = options.swiftPMOrDefault.traits {
335+
Set(traits)
336+
} else {
337+
nil
338+
}
339+
self.traitConfiguration = TraitConfiguration(enabledTraits: enabledTraits)
326340

327341
packageLoadingQueue.async {
328342
await orLog("Initial package loading") {

Sources/Diagnose/ReduceCommand.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ package struct ReduceCommand: AsyncParsableCommand {
5050
)
5151
var predicate: String?
5252

53-
private var nsPredicate: NSPredicate? { predicate.map { NSPredicate(format: $0) } }
53+
private var nsPredicate: NSPredicate? {
54+
guard let predicate else {
55+
return nil
56+
}
57+
return NSPredicate(format: predicate)
58+
}
5459
#else
5560
private var nsPredicate: NSPredicate? { nil }
5661
#endif

Sources/Diagnose/ReduceFrontendCommand.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ package struct ReduceFrontendCommand: AsyncParsableCommand {
4040
)
4141
var predicate: String?
4242

43-
private var nsPredicate: NSPredicate? { predicate.map { NSPredicate(format: $0) } }
43+
private var nsPredicate: NSPredicate? {
44+
guard let predicate else {
45+
return nil
46+
}
47+
return NSPredicate(format: predicate)
48+
}
4449
#else
4550
private var nsPredicate: NSPredicate? { nil }
4651
#endif

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
521521
indexFiles: indexFiles,
522522
buildSettings: buildSettings,
523523
processArguments: args,
524-
workingDirectory: buildSettings.workingDirectory.map(AbsolutePath.init(validating:))
524+
workingDirectory: buildSettings.workingDirectoryPath
525525
)
526526
case .singleFile(let file, let buildSettings):
527527
// We only end up in this case if the file's build settings didn't contain `-index-unit-output-path` and the build
@@ -532,7 +532,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
532532
indexFiles: [file.mainFile],
533533
buildSettings: buildSettings,
534534
processArguments: args,
535-
workingDirectory: buildSettings.workingDirectory.map(AbsolutePath.init(validating:))
535+
workingDirectory: buildSettings.workingDirectoryPath
536536
)
537537
}
538538
}
@@ -556,7 +556,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
556556
indexFiles: [uri],
557557
buildSettings: buildSettings,
558558
processArguments: args,
559-
workingDirectory: buildSettings.workingDirectory.map(AbsolutePath.init(validating:))
559+
workingDirectory: buildSettings.workingDirectoryPath
560560
)
561561
}
562562

@@ -780,3 +780,14 @@ fileprivate extension Process {
780780
}
781781
}
782782
}
783+
784+
fileprivate extension FileBuildSettings {
785+
var workingDirectoryPath: AbsolutePath? {
786+
get throws {
787+
guard let workingDirectory else {
788+
return nil
789+
}
790+
return try AbsolutePath(validating: workingDirectory)
791+
}
792+
}
793+
}

Sources/SourceKitD/SKDResponseDictionary.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ package final class SKDResponseDictionary: Sendable {
3636
}
3737

3838
package subscript(key: sourcekitd_api_uid_t) -> String? {
39-
return sourcekitd.api.variant_dictionary_get_string(dict, key).map(String.init(cString:))
39+
guard let cString = sourcekitd.api.variant_dictionary_get_string(dict, key) else {
40+
return nil
41+
}
42+
return String(cString: cString)
4043
}
4144

4245
package subscript(key: sourcekitd_api_uid_t) -> Int? {

Sources/SwiftLanguageService/Diagnostic.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,13 @@ extension DiagnosticStage {
442442
case sourcekitd.values.semaDiagStage:
443443
self = .sema
444444
default:
445-
let desc = sourcekitd.api.uid_get_string_ptr(uid).map { String(cString: $0) }
446-
logger.fault("Unknown diagnostic stage \(desc ?? "nil", privacy: .public)")
445+
let uidDescription =
446+
if let cString = sourcekitd.api.uid_get_string_ptr(uid) {
447+
String(cString: cString)
448+
} else {
449+
"<nil>"
450+
}
451+
logger.fault("Unknown diagnostic stage \(uidDescription, privacy: .public)")
447452
return nil
448453
}
449454
}

Sources/SwiftLanguageService/SemanticTokens.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ extension SwiftLanguageService {
6262
let semanticTokens = await orLog("Loading semantic tokens") { try await semanticHighlightingTokens(for: snapshot) }
6363

6464
let range =
65-
if let range = range.flatMap({ snapshot.byteSourceRange(of: $0) }) {
66-
range
65+
if let range {
66+
snapshot.byteSourceRange(of: range)
6767
} else {
6868
await tree.range
6969
}

Sources/SwiftLanguageService/SwiftLanguageService.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,17 @@ extension SwiftLanguageService {
651651
// Check that the document wasn't modified while we were getting diagnostics. This could happen because we are
652652
// calling `publishDiagnosticsIfNeeded` outside of `messageHandlingQueue` and thus a concurrent edit is
653653
// possible while we are waiting for the sourcekitd request to return a result.
654+
let latestVersionString =
655+
if let version = latestSnapshotID?.version {
656+
String(version)
657+
} else {
658+
"<nil>"
659+
}
654660
logger.log(
655661
"""
656662
Document was modified while loading diagnostics. \
657663
Loaded diagnostics for \(snapshot.id.version, privacy: .public), \
658-
latest snapshot is \((latestSnapshotID?.version).map(String.init) ?? "<nil>", privacy: .public)
664+
latest snapshot is \(latestVersionString, privacy: .public)
659665
"""
660666
)
661667
throw CancellationError()

Sources/SwiftLanguageService/SwiftTestingScanner.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,12 @@ final class SyntacticSwiftTestingTestScanner: SyntaxVisitor {
232232
let suiteAttribute = node.attributes
233233
.compactMap { $0.as(AttributeSyntax.self) }
234234
.first { $0.isNamed("Suite", inModuleNamed: "Testing") }
235-
let attributeData = suiteAttribute.map(TestingAttributeData.init(attribute:))
235+
let attributeData: TestingAttributeData? =
236+
if let suiteAttribute {
237+
TestingAttributeData(attribute: suiteAttribute)
238+
} else {
239+
nil
240+
}
236241

237242
if attributeData?.isHidden ?? false {
238243
return .skipChildren

Sources/SwiftLanguageService/SyntaxHighlightingTokenParser.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,13 @@ struct SyntaxHighlightingTokenParser {
203203
values.stringInterpolation
204204
]
205205
if !ignoredKinds.contains(uid) {
206-
let name = api.uid_get_string_ptr(uid).map(String.init(cString:))
207-
logger.error("Unknown token kind: \(name ?? "?", privacy: .public)")
206+
let name =
207+
if let cString = api.uid_get_string_ptr(uid) {
208+
String(cString: cString)
209+
} else {
210+
"<nil>"
211+
}
212+
logger.error("Unknown token kind: \(name, privacy: .public)")
208213
}
209214
return nil
210215
}

0 commit comments

Comments
 (0)