languages/swift.md
language: swift extensions: [".swift"]
Swift — Language-Specific Review Notes
Load this file alongside rules/universal.md. Universal rules are not repeated here — only Swift-specific rules and idioms.
PR Analyzer — Swift Risk Signals
print()statements left in production code- Force unwrap (
!) on optionals outside of tests or justified init - Force cast (
as!) without a safe fallback - Hardcoded credentials or API keys in source
Code Quality — Swift Checks
- Force unwrap (
!) used broadly — preferguard letorif let try!used outside of guaranteed-safe contexts- Retain cycles in closures — missing
[weak self]or[unowned self] @objc/dynamicused without an Objective-C interop reason
Security
- Flag credentials stored in
UserDefaults— require Keychain - Flag
URLSessionrequests over plain HTTP in production - Flag
WKWebViewloading arbitrary user-supplied URLs without validation
Async / Concurrency
- Flag
DispatchQueue.main.synccalled from the main thread — deadlock - Flag
@escapingclosures capturingselfstrongly in reference cycles — use[weak self] - Flag mixing
async/awaitandDispatchQueuefor the same operation without clear reasoning - Flag
Task { }(unstructured) where a structuredasync letorTaskGroupwould maintain structure - Flag data races — shared mutable state accessed from multiple tasks without an actor
Resource Management
- Flag
URLSessionDataTaskstarted with no cancellation handle stored — cannot be cancelled if the view disappears - Flag
NotificationCenterobservers added without a correspondingremoveObserver— memory leak - Flag
CLLocationManager/AVCaptureSessionnot stopped when the owning view controller is dismissed
Exception Handling
- Flag
try!outside of guaranteed-safe contexts (test fixtures, constants) — crashes on failure - Flag
try?discarding errors where the failure mode matters to the caller - Flag error types conforming to
Errorwith no associated values or message — makes debugging hard - Flag throwing functions calling
fatalError()as a fallback — choose one error strategy
Performance
- Flag
UIImage(named:)called repeatedly for the same asset without caching - Flag synchronous network calls on the main thread
- Flag
Arrayused for frequent membership tests — preferSet - Flag
Stringinterpolation inside tight loops where a pre-built string would avoid allocations
Idioms and Best Practices
Optionals
- Prefer
guard letfor early exit;if letfor local scope - Prefer optional chaining (
?.) over force unwrap - Flag implicitly unwrapped optionals (
var x: String!) outside of@IBOutlet
Memory Management
- Flag closures capturing
selfstrongly in reference cycles — use[weak self] - Prefer
structoverclassfor value semantics unless identity or inheritance is needed - Use
unownedonly when the lifetime is guaranteed — otherwiseweak
Concurrency (Swift 5.5+)
- Prefer
async/awaitover completion handlers in new code - Flag
DispatchQueue.main.asyncwhere@MainActororawait MainActor.runis more appropriate