Repository navigation
Replace force-unwrapped .data(using: .utf8)! with Data(string.utf8) - #1834
Conversation
Four call sites used `line.data(using: .utf8)!` to convert a String to Data for FileHandle.write(). While .utf8 encoding never fails for Swift strings (making the force unwrap safe in practice), `Data(line.utf8)` is the idiomatic Swift equivalent that avoids the force unwrap entirely. Changed files: - GhosttyTerminalView.swift (3 occurrences: init log, surface log, size log) - WorkspaceContentView.swift (1 occurrence: panel debug log) Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
@lifrary is attempting to deploy a commit to the Manaflow Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughRefactored debug file logging across two files to replace force-unwrapped UTF-8 data conversion with safer Data initialization. Changed Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Greptile SummaryThis is a focused, mechanical cleanup that replaces four
Confidence Score: 5/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["log helper called\n(initLog / surfaceLog / sizeLog / debugPanelLookup)"] --> B["FileHandle(forWritingAtPath: path)"]
B -->|handle exists| C["handle.seekToEndOfFile()"]
C --> D["handle.write(Data(line.utf8))\n✅ was: line.data(using: .utf8)!"]
D --> E["handle.closeFile()"]
B -->|nil — file absent| F["FileManager.default.createFile(\n atPath: path,\n contents: line.data(using: .utf8)\n)\n⬅ unchanged (returns Data?, no ! needed)"]
Last reviewed commit: "Replace force-unwrap..." |
|
Thank you for the contribution! |
…wrapped-utf8-data-encoding Replace force-unwrapped .data(using: .utf8)! with Data(string.utf8)
Summary
Four call sites convert a
StringtoDataforFileHandle.write()using.data(using: .utf8)!. While this force unwrap is safe in practice (UTF-8 encoding never fails for Swift strings),Data(string.utf8)is the idiomatic Swift equivalent that eliminates the force unwrap entirely — shorter, clearer, and no!needed.Before / After
Both produce identical
Dataoutput.String.utf8returns aString.UTF8ViewwhichData.init(_:)accepts directly.What changed
4 lines across 2 files:
GhosttyTerminalView.swiftGhosttyAppinit log writerGhosttyTerminalView.swiftTerminalSurfacesurface log writerGhosttyTerminalView.swiftTerminalSurfacesize log writerWorkspaceContentView.swiftAll four are the same pattern:
FileHandle.seekToEndOfFile()→FileHandle.write(...)→FileHandle.closeFile()inside#if DEBUGor debug-only logging paths.Why not change the
elsebranch too?Each call site has a corresponding
elsebranch:This call uses
.data(using: .utf8)without the!(it returnsData?, andcreateFile(contents:)acceptsData?), so there is no force unwrap to remove. Left as-is to keep this PR focused on force unwrap elimination only.Impact
Data(string.utf8)produces identical bytes to.data(using: .utf8)!!count in the codebase, improving code safety postureTest plan
/tmp/cmux-debug.log, surface logs) still receive log output after the changegrep -rn '.data(using: .utf8)!' Sources/returns no remaining force-unwrapped.utf8encoding calls🤖 Generated with Claude Code
Summary by cubic
Replaced force-unwrapped UTF-8 conversions with
Data(string.utf8)in debug log writers. This removes!usage while keeping behavior identical..data(using: .utf8)!forData(string.utf8)at 4 call sites inGhosttyTerminalView.swiftandWorkspaceContentView.swift..data(using: .utf8)increateFile(contents:)calls as-is.Written for commit 8ad794f. Summary will update on new commits.
Summary by CodeRabbit