-
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
Reverse Data -> String conversion rule #5601
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,7 +10,10 @@ | |||||||||||||||
|
||||||||||||||||
#### Enhancements | ||||||||||||||||
|
||||||||||||||||
* None. | ||||||||||||||||
* Add new `optional_data_string_conversion` rule to enforce | ||||||||||||||||
failable conversions of `Data` -> UTF-8 `String`. | ||||||||||||||||
[Sam Rayner](https://github.com/samrayner) | ||||||||||||||||
[#5263](https://github.com/realm/SwiftLint/issues/5263) | ||||||||||||||||
|
||||||||||||||||
#### Bug Fixes | ||||||||||||||||
|
||||||||||||||||
|
@@ -24,6 +27,11 @@ | |||||||||||||||
[SimplyDanny](https://github.com/SimplyDanny) | ||||||||||||||||
[#5153](https://github.com/realm/SwiftLint/issues/5153) | ||||||||||||||||
|
||||||||||||||||
* Revert `optional_data_string_conversion` enforcing | ||||||||||||||||
non-failable conversions of `Data` -> UTF-8 `String`. | ||||||||||||||||
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please append two spaces to the end of the description. They are required to enforce a line break in the rendered Markdown. Also, this should go into the "Breaking" section. |
||||||||||||||||
[Sam Rayner](https://github.com/samrayner) | ||||||||||||||||
[#5263](https://github.com/realm/SwiftLint/issues/5263) | ||||||||||||||||
|
||||||||||||||||
## 0.55.1: Universal Washing Powder | ||||||||||||||||
|
||||||||||||||||
#### Breaking | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,16 +5,14 @@ struct NonOptionalStringDataConversionRule: Rule { | |||||
var configuration = SeverityConfiguration<Self>(.warning) | ||||||
static let description = RuleDescription( | ||||||
identifier: "non_optional_string_data_conversion", | ||||||
name: "Non-Optional String <-> Data Conversion", | ||||||
description: "Prefer using UTF-8 encoded strings when converting between `String` and `Data`", | ||||||
name: "Non-Optional String -> Data Conversion", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
description: "Prefer using non-optional Data(_:) when converting a UTF-8 String to Data", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
kind: .lint, | ||||||
nonTriggeringExamples: [ | ||||||
Example("Data(\"foo\".utf8)"), | ||||||
Example("String(decoding: data, as: UTF8.self)") | ||||||
], | ||||||
triggeringExamples: [ | ||||||
Example("\"foo\".data(using: .utf8)"), | ||||||
Example("String(data: data, encoding: .utf8)") | ||||||
] | ||||||
) | ||||||
} | ||||||
|
@@ -31,15 +29,6 @@ private extension NonOptionalStringDataConversionRule { | |||||
violations.append(node.positionAfterSkippingLeadingTrivia) | ||||||
} | ||||||
} | ||||||
|
||||||
override func visitPost(_ node: DeclReferenceExprSyntax) { | ||||||
if node.baseName.text == "String", | ||||||
let parent = node.parent?.as(FunctionCallExprSyntax.self), | ||||||
parent.arguments.map({ $0.label?.text }) == ["data", "encoding"], | ||||||
parent.arguments.last?.expression.as(MemberAccessExprSyntax.self)?.isUTF8 == true { | ||||||
violations.append(node.positionAfterSkippingLeadingTrivia) | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,31 @@ | ||||||
import SwiftSyntax | ||||||
|
||||||
@SwiftSyntaxRule | ||||||
struct OptionalDataStringConversionRule: Rule { | ||||||
var configuration = SeverityConfiguration<Self>(.warning) | ||||||
static let description = RuleDescription( | ||||||
identifier: "optional_data_string_conversion", | ||||||
name: "Optional Data -> String Conversion", | ||||||
description: "Prefer using failable String(data:encoding:) when converting from `Data` to a UTF-8 `String`", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
kind: .lint, | ||||||
nonTriggeringExamples: [ | ||||||
Example("String(data: data, encoding: .utf8)") | ||||||
], | ||||||
triggeringExamples: [ | ||||||
Example("String(decoding: data, as: UTF8.self)") | ||||||
] | ||||||
) | ||||||
} | ||||||
|
||||||
private extension OptionalDataStringConversionRule { | ||||||
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { | ||||||
override func visitPost(_ node: DeclReferenceExprSyntax) { | ||||||
if node.baseName.text == "String", | ||||||
let parent = node.parent?.as(FunctionCallExprSyntax.self), | ||||||
parent.arguments.map({ $0.label?.text }) == ["decoding", "as"], | ||||||
parent.arguments.last?.expression.as(IdentifierTypeSyntax.self)?.name == "UTF8.self" { | ||||||
samrayner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
violations.append(node.positionAfterSkippingLeadingTrivia) | ||||||
} | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please append two spaces to the end of the description. They are required to enforce a line break in the rendered Markdown.