Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
[Hokila](https://github.com/Hokila)
[#6897](https://github.com/realm/SwiftLint/issues/6897)

* Stop `syntactic_sugar` from reporting and correcting types referenced with a
module selector other than `Swift::`, such as `Foo::Array<Int>`, and report
`Swift::Array<Int>()` in expressions.
[gxlactuss](https://github.com/gxlactuss)
[#6924](https://github.com/realm/SwiftLint/issues/6924)

## 0.65.1: Fresh Folded Fixtures

### Breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ private enum SugaredType: String {
var typeName = typeName
if typeName.hasPrefix("Swift.") {
typeName.removeFirst("Swift.".count)
} else if typeName.hasPrefix("Swift::") {
typeName.removeFirst("Swift::".count)
}

self.init(rawValue: typeName)
Expand Down Expand Up @@ -195,7 +197,9 @@ private final class SyntacticSugarRuleVisitor: SyntaxVisitor {
}

if let simpleType = typeSyntax?.as(IdentifierTypeSyntax.self) {
if SugaredType(typeName: simpleType.name.text) != nil {
// A module selector other than `Swift::` refers to a type from that module, e.g. `Foo::Array<Int>`
let isSwiftType = simpleType.moduleSelector.map { $0.moduleName.text == "Swift" } ?? true
if isSwiftType, SugaredType(typeName: simpleType.name.text) != nil {
return violation(from: simpleType)
}

Expand Down Expand Up @@ -342,7 +346,7 @@ extension IdentifierTypeSyntax: SyntaxWithGenericClause {

extension GenericSpecializationExprSyntax: SyntaxWithGenericClause {
var typeName: String? {
expression.as(DeclReferenceExprSyntax.self)?.firstToken(viewMode: .sourceAccurate)?.text ??
expression.as(DeclReferenceExprSyntax.self)?.baseName.text ??
expression.as(MemberAccessExprSyntax.self)?.declName.baseName.text
}
var genericArguments: GenericArgumentClauseSyntax? { genericArgumentClause }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal enum SyntacticSugarRuleExamples {
"let x = case Optional<Any>.none = obj",
"let a = Swift.Optional<String?>.none",
"func f() -> [Array<Int>.Index] { [Array<Int>.Index]() }".asExample(excludeFromDocumentation: true),
"let x: Foo::Array<Int>".asExample(excludeFromDocumentation: true),
"func x(a: Foo::Optional<Int>) -> Foo::Dictionary<String, Int>".asExample(excludeFromDocumentation: true),
"let x = Foo::Array<Int>()".asExample(excludeFromDocumentation: true),
])

static let triggering = #examples([
Expand Down Expand Up @@ -65,6 +68,9 @@ internal enum SyntacticSugarRuleExamples {
let dict: [String: Any] = [:]
_ = dict["key"] as? ↓Optional<String?> ?? Optional<String?>.none
""",
"let x: ↓Swift::Array<String>".asExample(excludeFromDocumentation: true),
"let x: Foo::Array<↓Array<Int>>".asExample(excludeFromDocumentation: true),
"let x = ↓Swift::Dictionary<Int, String>()".asExample(excludeFromDocumentation: true),
])

static let corrections = #corrections([
Expand All @@ -84,5 +90,7 @@ internal enum SyntacticSugarRuleExamples {
"let x:[[[Int: Int]: Int]: String]",
"let x:↓Array<↓Dictionary<Int, Int>>": "let x:[[Int: Int]]",
"let x:↓Optional<↓Dictionary<Int, Int>>": "let x:[Int: Int]?",
"let x: Swift::Optional<String>": "let x: String?",
"let x = Swift::Array<Int>()": "let x = [Int]()",
])
}