Skip to content

Fix double space between catch/where in where-only catch clause#1203

Merged
allevato merged 1 commit into
swiftlang:mainfrom
broken-circle:catch-where-spaces-1076
May 14, 2026
Merged

Fix double space between catch/where in where-only catch clause#1203
allevato merged 1 commit into
swiftlang:mainfrom
broken-circle:catch-where-spaces-1076

Conversation

@broken-circle
Copy link
Copy Markdown
Contributor

Closes #1076.

When formatting a catch clause whose item consists solely of a where clause with no preceding pattern, TokenStreamCreator.visit(_ node: CatchClauseSyntax) emits a .space before the catch item's first token. With no pattern present, the first token is the where keyword. TokenStreamCreator.visit(_ node: WhereClauseSyntax) also inserts a preceding break before the same where keyword, producing duplicate whitespace.

This PR suppresses the catch-clause-level whitespace when a catch item has no pattern and allows WhereClauseSyntax to remain the sole source.

Testing

Added testCatchWhereOnly_noBreakBeforeCatch() and testCatchWhereOnly_breakBeforeCatch() to DoStmtTests.

All tests pass when run locally on macOS.

When a `catch` clause's item consists solely of a `where` clause with
no preceding pattern, the `CatchClauseSyntax` visitor inserts a space
before the `catch` item's first token. With no pattern present, the
first token is the `where` keyword. The `WhereClauseSyntax` visitor
also inserts a preceding break before the same `where` keyword,
producing duplicate whitespace.

Suppress the `catch`-clause-level whitespace when a `catch` item has no
pattern and allow `WhereClauseSyntax` to remain the sole source.
Copy link
Copy Markdown
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@allevato allevato merged commit b6808a1 into swiftlang:main May 14, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Two spaces inserted between catch and where if there is no clause to capture the error

2 participants