Skip to content

Commit bda1387

Browse files
committed
Refactor rename to avoid a force unwrap
1 parent b381811 commit bda1387

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

Sources/SourceKitLSP/Rename.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,7 @@ extension SourceKitServer {
298298

299299
// Determine the local edits and the USR to rename
300300
let renameResult = try await languageService.rename(request)
301-
var edits = renameResult.edits
302-
if edits.changes == nil {
303-
// Make sure `edits.changes` is non-nil so we can force-unwrap it below.
304-
edits.changes = [:]
305-
}
301+
var changes = renameResult.edits.changes ?? [:]
306302

307303
if let usr = renameResult.usr, let oldName = renameResult.oldName, let index = workspace.index {
308304
// If we have a USR + old name, perform an index lookup to find workspace-wide symbols to rename.
@@ -324,7 +320,7 @@ extension SourceKitServer {
324320
await withTaskGroup(of: (DocumentURI, [TextEdit])?.self) { taskGroup in
325321
for (url, renameLocations) in locationsByFile {
326322
let uri = DocumentURI(url)
327-
if edits.changes![uri] != nil {
323+
if changes[uri] != nil {
328324
// We already have edits for this document provided by the language service, so we don't need to compute
329325
// rename ranges for it.
330326
continue
@@ -358,11 +354,13 @@ extension SourceKitServer {
358354
}
359355
}
360356
for await case let (uri, textEdits)? in taskGroup where !textEdits.isEmpty {
361-
precondition(edits.changes![uri] == nil, "We should create tasks for URIs that already have edits")
362-
edits.changes![uri] = textEdits
357+
precondition(changes[uri] == nil, "We should not create tasks for URIs that already have edits")
358+
changes[uri] = textEdits
363359
}
364360
}
365361
}
362+
var edits = renameResult.edits
363+
edits.changes = changes
366364
return edits
367365
}
368366
}

0 commit comments

Comments
 (0)