-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Rule Request: Prefer non-optional UTF8 String <-> Data conversion #5263
Comments
I think there's an issue with the // works, an initializer that supports Sequence<UInt8> exists
let data = Data("foo".utf8)
// compiler error. no initializer for Data exists that supports Sequence<UInt16> out of the box
let data = Data("foo".utf16) |
Whoops! Thanks @ben-p-commits ! Reduced the scope of the request to just UTF8. |
@samrayner - wrapped up the day with this: #5264 |
I think this rule is misguided, dangerous, and based on some confusion around conversion between strings and byte sequences. While it is true that any let data = Data([0xe2, 0x28, 0xa1]) This is not a valid utf-8 sequence. If you try to convert it to a let string = String(decoding: data, as: UTF8.self) you will obtain the string In other words, using |
Thanks for the correction @ortekka ! Based on what you've said it sounds like Do you see any issues with using |
@samrayner No, I don't see any issues with those two examples, because in that case, My issue and warning has to do with the assumption that you can take an arbitrary byte sequence (as a |
Trip report: I don't build swift code super often, but had happened to have updated to a version with this in which then broke my build, and I uninstalled swiftlint to work around it. Would love to see this removed from the homebrew versions soon (as it's bad advice, as @ortekka has explained). |
Hopefully addresses the swift-lint errors. Fix via realm/SwiftLint#5263
I think this rule is too restrictive, consider this code: This triggers swiftlint warning, however due to swift guard mechanism, if any of the optional unwrap fails, code will not be executed further. |
Hopefully addresses the swift-lint errors. Fix via realm/SwiftLint#5263
Updates our version of SwiftLint, but opts out of one of the new rules because making the required changes: ```diff --- a/Libraries/Connect/Internal/Interceptors/GRPCWebInterceptor.swift +++ b/Libraries/Connect/Internal/Interceptors/GRPCWebInterceptor.swift @@ -260,17 +260,11 @@ extension GRPCWebInterceptor: StreamInterceptor { } } -private struct TrailersDecodingError: Error {} - private extension Trailers { static func fromGRPCHeadersBlock(_ source: Data) throws -> Self { - guard let string = String(data: source, encoding: .utf8) else { - throw TrailersDecodingError() - } - // Decode trailers based on gRPC Web spec: // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md - return string + return String(decoding: source, as: UTF8.self) .split(separator: "\r\n") .reduce(into: Trailers()) { trailers, line in guard let separatorIndex = line.firstIndex(of: ":") else { ``` Caused some tests to fail: ``` FAILED: gRPC-Web Unexpected Responses/HTTPVersion:1/TLS:false/trailers-in-body/unary/multiple-responses: actual error {code: 2 (unknown), message: "RPC response missing status"} does not match expected code 12 (unimplemented) ``` It looks like there was some discussion around this rule being potentially problematic here: realm/SwiftLint#5263 (comment) Signed-off-by: Michael Rebello <me@michaelrebello.com>
New Issue Checklist
New rule request
Please describe the rule idea, format
this issue's title as
Rule Request: [Rule Name]
and describe:the community thinks about this.
String.data(using:)
andString.init(data:encoding:)
return optional values due to supporting a large number of encodings, some of which can fail conversion.However, conversion of UTF8 encoded Strings cannot fail. E.g.
Data("foo".utf8)
andString(decoding: Data(), as: UTF8.self)
do not return Optionals.https://www.swiftbysundell.com/tips/converting-between-string-and-data-without-optionals/
Would trigger violation:
Would not trigger violation:
I don't think any configuration is necessary.
See README.md for guidelines on when to mark a rule as opt-in.
I suggest this can be on by default. I think non-optional is always preferable to optional where it is completely safe. There is no sensible handling that one can do for an optional returned using the violating methods as that code will never run.
The text was updated successfully, but these errors were encountered: