@@ -238,11 +238,12 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
238
238
) -> TypeSyntax {
239
239
var wrappedType = wrappedType
240
240
241
- // Function types and some-or-any types must be wrapped in parentheses before using shorthand
242
- // optional syntax, otherwise the "?" will bind incorrectly (in the function case it binds to
243
- // only the result, and in the some-or-any case it only binds to the child protocol). Attach the
244
- // leading trivia to the left-paren that we're adding in these cases.
241
+ // Certain types must be wrapped in parentheses before using shorthand optional syntax to avoid
242
+ // the "?" from binding incorrectly when re-parsed. Attach the leading trivia to the left-paren
243
+ // that we're adding in these cases.
245
244
switch Syntax ( wrappedType) . as ( SyntaxEnum . self) {
245
+ case . attributedType( let attributedType) :
246
+ wrappedType = parenthesizedType ( attributedType, leadingTrivia: leadingTrivia)
246
247
case . functionType( let functionType) :
247
248
wrappedType = parenthesizedType ( functionType, leadingTrivia: leadingTrivia)
248
249
case . someOrAnyType( let someOrAnyType) :
@@ -319,12 +320,11 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
319
320
) -> OptionalChainingExprSyntax ? {
320
321
guard var wrappedTypeExpr = expressionRepresentation ( of: wrappedType) else { return nil }
321
322
322
- // Function types and some-or-any types must be wrapped in parentheses before using shorthand
323
- // optional syntax, otherwise the "?" will bind incorrectly (in the function case it binds to
324
- // only the result, and in the some-or-any case it only binds to the child protocol). Attach the
325
- // leading trivia to the left-paren that we're adding in these cases.
323
+ // Certain types must be wrapped in parentheses before using shorthand optional syntax to avoid
324
+ // the "?" from binding incorrectly when re-parsed. Attach the leading trivia to the left-paren
325
+ // that we're adding in these cases.
326
326
switch Syntax ( wrappedType) . as ( SyntaxEnum . self) {
327
- case . functionType, . someOrAnyType:
327
+ case . attributedType , . functionType, . someOrAnyType:
328
328
wrappedTypeExpr = parenthesizedExpr ( wrappedTypeExpr, leadingTrivia: leadingTrivia)
329
329
default :
330
330
// Otherwise, the argument type can safely become an optional by simply appending a "?". If
@@ -448,6 +448,9 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
448
448
case . someOrAnyType( let someOrAnyType) :
449
449
return ExprSyntax ( TypeExprSyntax ( type: someOrAnyType) )
450
450
451
+ case . attributedType( let attributedType) :
452
+ return ExprSyntax ( TypeExprSyntax ( type: attributedType) )
453
+
451
454
default :
452
455
return nil
453
456
}
0 commit comments