-
Couldn't load subscription status.
- Fork 203
Description
Question
I'm analyzing a Swift String extension String.swift and found a guard condition that appears to be unreachable:
extension String {
func asciified() -> String? {
guard !isASCII else {
return self
}
guard !canBeConverted(to: .ascii) else {
return nil // ← This line seems unreachable
}
// ... CFStringTransform logic
}
}My Testing
I tested extensively with various non-ASCII strings, and ALL returned canBeConverted(to: .ascii) = false:
- Accented characters: "café", "naïve", "résumé" → false
- Cyrillic: "Привет", "Москва" → false
- Emojis: "🚀", "😀😃😄" → false
- Japanese: "こんにちは" → false
- Special Unicode: "—", "…", "«»" → false
- Fullwidth characters: "A", "123" → false
- Combining characters: "e\u{0301}" (é) → false
- Control characters: BOM, Zero Width Space, etc. → false
Questions
- Are there any real-world cases where a non-ASCII string returns canBeConverted(to: .ascii) = true?
- Is this guard condition effectively "dead code"?
- What was the original intention of this check?
Context
I discovered this while integrating Matrix into my own messaging app. These lines puzzled me and took over an hour to analyze, which made me realize this might be confusing other developers too.
The function works perfectly without this guard - all test strings are successfully transliterated by CFStringTransform. I'm trying to understand if this condition serves a purpose I'm missing or if it can be safely removed.
Any insights would be greatly appreciated!
If this is indeed dead code, I'd be happy to submit a PR to remove it.