-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optional_string_data_conversion
's Data
-> String
rule should be optional
#5785
Comments
non_optional_string_data_conversion
's Data
-> String
rule should be optionaloptional_string_data_conversion
's Data
-> String
rule should be optional
Additionally, since there's no way for Swift to convey the fact that a |
I understand that in your case, there are a lot of measures and implicit knowledge in place suggesting that some data is valid UTF-8. That's not necessarily the case in other code bases, though. In your case, you'd go and disable the rule in your configuration because you know better. Others might like the rule pointing them to potential issues by default and leave it enabled. Deciding about adding a rule as opt-in or enabled-by-default is always opinionated. Luckily, everything is configurable for your own best fit. Changing defaults of a rule after it has been released causes some trouble as well. I don't think that's worth it since there are no hard objective arguments. |
Except there are "hard objective arguments" about default rules, both in general and this one in particular. SwiftLint's README currently says this about opt-in rules:
First, in all my codebases it has a 100% false positive rate. This includes everything from high level apps to low level libraries. And given there's no way for the rule to know ahead of time whether the Second, I'm not sure what consensus means to this project, but the rule was enabled as a default through a single PR with little apparent discussion. But it certainly fails the second part of the guideline, as it's only really useful with dynamic strings (not round tripped literals or other content) produced by systems which don't default to UTF-8 (increasingly rare) where failure is desired over repair (not often the case in UI presenting code, perhaps the case in low level systems) and the developer is unaware of the behavior of So, by SwiftLint's own guidelines, this should be an opt-in rule.
By that logic all rules should be opt-out, since everything's configurable. Obviously SwiftLint has to have some guidelines around what rules should be opt-in or out. It just doesn't seem like they were applied in this case.
Changing a rule to opt-in isn't breaking, and given the rule was added as a default, which is far more invasive, shipping a quick follow up release to turn it off seems better than the alternative. At worst people would've made some things optional that didn't need to be (though I expect the vast majority would just turn the rule off, given some thought). |
Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion) Discussion on this: realm/SwiftLint#5785
Optional Data -> String Conversion Violation: Prefer failable `String(data:encoding:)` initializer when converting `Data` to `String` (optional_data_string_conversion) Discussion on this: realm/SwiftLint#5785
New Issue Checklist
Feature or Enhancement Proposal
In 0.57.0, the
non_optional_string_data_conversion
rule was split: theData
->String
conversion check was actually inverted and made its own rule. Unfortunately, as a default rule it's very heavy handed. In the vast majority of Swift apps, usingString(decoding:as:)
is perfectly correct, as non-UTF-8 strings are increasingly rare, and there often aren't any arbitrary strings at all. Additionally, using this initializer is an explicit acknowledgment that we don't care if the string isn't fully UTF-8. That is, we don't care if the decoding placeholder is inserted. That's may be a bug, but a far more minor one than not handling a string at all.This part of the rule made optional by default.
The text was updated successfully, but these errors were encountered: