@@ -238,11 +238,12 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
238238 ) -> TypeSyntax {
239239 var wrappedType = wrappedType
240240
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.
245244 switch Syntax ( wrappedType) . as ( SyntaxEnum . self) {
245+ case . attributedType( let attributedType) :
246+ wrappedType = parenthesizedType ( attributedType, leadingTrivia: leadingTrivia)
246247 case . functionType( let functionType) :
247248 wrappedType = parenthesizedType ( functionType, leadingTrivia: leadingTrivia)
248249 case . someOrAnyType( let someOrAnyType) :
@@ -319,12 +320,11 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
319320 ) -> OptionalChainingExprSyntax ? {
320321 guard var wrappedTypeExpr = expressionRepresentation ( of: wrappedType) else { return nil }
321322
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.
326326 switch Syntax ( wrappedType) . as ( SyntaxEnum . self) {
327- case . functionType, . someOrAnyType:
327+ case . attributedType , . functionType, . someOrAnyType:
328328 wrappedTypeExpr = parenthesizedExpr ( wrappedTypeExpr, leadingTrivia: leadingTrivia)
329329 default :
330330 // Otherwise, the argument type can safely become an optional by simply appending a "?". If
@@ -448,6 +448,9 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
448448 case . someOrAnyType( let someOrAnyType) :
449449 return ExprSyntax ( TypeExprSyntax ( type: someOrAnyType) )
450450
451+ case . attributedType( let attributedType) :
452+ return ExprSyntax ( TypeExprSyntax ( type: attributedType) )
453+
451454 default :
452455 return nil
453456 }
0 commit comments