Add detection of leading-dot when the contextual type is String to optional_data_string_conversion` rule#6372
Conversation
Generated by 🚫 Danger |
SimplyDanny
left a comment
There was a problem hiding this comment.
Thanks for preparing the PR! Please consider my comments.
804ec4c to
4cfbd90
Compare
|
Hey @nadeemnali, is anything blocking you from moving this forward? |
Hey @SimplyDanny Apologies, I was busy for past weeks, will update the comments ASAP |
|
@SimplyDanny Can you please help me with the danger issue in pipeline, can't seem to find any logs for the failure |
Danger doesn't like merge commits. Please rebase your branch onto main instead. |
5de3ae6 to
a7dd1f5
Compare
String.init(decoding: data, as: UTF8.self) and let text: String = .init(decoding: data, as: UTF8.self) to optional_data_string_conversion rule
String.init(decoding: data, as: UTF8.self) and let text: String = .init(decoding: data, as: UTF8.self) to optional_data_string_conversion rule to optional_data_string_conversion` rule
bd3286e to
13b6617
Compare
SimplyDanny
left a comment
There was a problem hiding this comment.
Thanks for addressing my remarks! There is one more thing about allowing .init(...) even without an explicit type optionally.
5160e5b to
7b74331
Compare
6098bab to
6faf8d2
Compare
|
Hi @SimplyDanny I have updated all the comments, can you please review them |
6faf8d2 to
167a8d4
Compare
…ting the arbitrary tree walking.
e7343b8 to
2ea1dc3
Compare
a927a8f to
d2b1515
Compare
Summary
The
optional_data_string_conversionrule now detects additional variant patterns ofString(decoding:as:)initialization:
String(decoding: data, as: UTF8.self)(already supported)String.init(decoding: data, as: UTF8.self)(NEW)let text: String = .init(decoding: data, as: UTF8.self)(NEW)let text = .init(decoding: data, as: UTF8.self)(controlled byallow_implicit_initconfig)Changes
Refactored visitor logic (
OptionalDataStringConversionRule.swift):visitPost(_ node: DeclReferenceExprSyntax)tovisitPost(_ node: FunctionCallExprSyntax)forbetter detection of all call patterns
String.init(...)(MemberAccessExpr with baseString).init(...)when assigned to a variable with explicitStringtypeannotation
Added configuration system (new file:
OptionalDataStringConversionConfiguration.swift):allow_implicit_init(default:false)false(default): only flag.init(...)with explicitStringtype annotationtrue: also flag implicit leading-dot.init(...)without type annotationEnhanced rule documentation:
String.init(...)and.init(...)with explicit typeallow_implicit_initconfiguration optionUpdated configuration registry (
default_rule_configurations.yml):allow_implicit_init: falseto default configurationWhy
Fixes #6359 where cases like
let text: String = .init(decoding: data, as: UTF8.self)were not detected.Testing
Run the rule's example code with:
swift test --filter OptionalDataStringConversionRule Or lint against the rule directly: ./.build/debug/swiftlint lint Source/SwiftLintBuiltInRules/Rules/Lint/OptionalDataStringConversionRule.swift