Skip to content

Commit 3bb293b

Browse files
authored
Merge pull request swiftlang#256 from google/format-shorthand
Fix false diagnostics in UseShorthandTypeNames.
2 parents 303414d + 96af76b commit 3bb293b

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

Sources/SwiftFormatRules/UseShorthandTypeNames.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,30 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
3232
!(parent is MemberTypeIdentifierSyntax) else { return node }
3333
// Type is in long form if it has a non-nil generic argument clause
3434
guard let genArg = node.genericArgumentClause else { return node }
35-
diagnose(.useTypeShorthand(type: node.name.text.lowercased()), on: node)
3635

3736
// Ensure that all arguments in the clause are shortened and in expected-format by visiting
3837
// the argument list, first
3938
let argList = visit(genArg.arguments) as! GenericArgumentListSyntax
4039
// Store trivia of the long form type to pass to the new shorthand type later
4140
let trivia = retrieveTrivia(from: node)
4241

42+
let newNode: TypeSyntax
4343
switch node.name.text {
4444
case "Array":
4545
guard let arg = argList.firstAndOnly else { return node }
46-
let newArray = shortenArrayType(argument: arg, trivia: trivia)
47-
return newArray
46+
newNode = shortenArrayType(argument: arg, trivia: trivia)
4847
case "Dictionary":
4948
guard let args = exactlyTwoChildren(of: argList) else { return node }
50-
let newDictionary = shortenDictionaryType(arguments: args, trivia: trivia)
51-
return newDictionary
49+
newNode = shortenDictionaryType(arguments: args, trivia: trivia)
5250
case "Optional":
5351
guard let arg = argList.firstAndOnly else { return node }
54-
let newOptional = shortenOptionalType(argument: arg, trivia: trivia)
55-
return newOptional
52+
newNode = shortenOptionalType(argument: arg, trivia: trivia)
5653
default:
57-
break
54+
return node
5855
}
59-
return node
56+
57+
diagnose(.useTypeShorthand(type: node.name.text), on: node)
58+
return newNode
6059
}
6160

6261
// Visits all potential long forms interpreted as expressions

Tests/SwiftFormatRulesTests/UseShorthandTypeNamesTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class UseShorthandTypeNamesTests: DiagnosingTestCase {
2323
y: Dictionary<Array<Optional<String>>, Optional<Int>>) {
2424
Dictionary<Array<Int>.Index, String>.init()
2525
Dictionary<String, Optional<Float>>.init()
26+
UnsafePointer<UInt8>.init()
27+
UnsafeMutablePointer<UInt8>.init()
2628
}
2729
""",
2830
expected: """
@@ -40,7 +42,14 @@ public class UseShorthandTypeNamesTests: DiagnosingTestCase {
4042
y: [[String?]: Int?]) {
4143
[Array<Int>.Index: String].init()
4244
[String: Float?].init()
45+
UnsafePointer<UInt8>.init()
46+
UnsafeMutablePointer<UInt8>.init()
4347
}
4448
""")
49+
XCTAssertDiagnosed(.useTypeShorthand(type: "Array"))
50+
XCTAssertDiagnosed(.useTypeShorthand(type: "Dictionary"))
51+
XCTAssertDiagnosed(.useTypeShorthand(type: "Optional"))
52+
XCTAssertNotDiagnosed(.useTypeShorthand(type: "UnsafePointer"))
53+
XCTAssertNotDiagnosed(.useTypeShorthand(type: "UnsafeMutablePointer"))
4554
}
4655
}

0 commit comments

Comments
 (0)