Open HHuckebein opened 1 week ago
That's the idea of the rule. It asks you to use the failable initializer to make you think about the error case. Your current code will just fail at runtime in case data
cannot be converted.
The object created by try encode() is non-optional and String(decoding: data, as: UTF8.self) is a non-failing initializer.
String(decoding: data, as: UTF8.self)
String(decoding: data, as: UTF8.self)
may panic at runtime if data
cannot be represented as UTF-8.
New Issue Checklist
Bug Description
When using the code below, SwiftLint 0.57.0 reports an error like this Optional Data -> String Conversion Violation: Prefer failable
String(data:encoding:)
initializer when convertingData
toString
(optional_data_string_conversion)but following the advice leads to Value of optional type 'String?' must be unwrapped to a value of type 'String'.
Mention the command or other SwiftLint integration method that caused the issue. Include stack traces or command output.
None
Environment
swiftlint version
to be sure) 0.57.0xcodebuild -version
to be sure) Xcode 16.2 Build version 16B5100eAre you using nested configurations? If so, paste their relative paths and respective contents.