Skip to content

Enforce braces on all control-flow bodies in the C# emitter (cop)#11062

Open
JoshLove-msft wants to merge 2 commits into
microsoft:mainfrom
JoshLove-msft:joshlove-msft/upgrade-braces-cop
Open

Enforce braces on all control-flow bodies in the C# emitter (cop)#11062
JoshLove-msft wants to merge 2 commits into
microsoft:mainfrom
JoshLove-msft:joshlove-msft/upgrade-braces-cop

Conversation

@JoshLove-msft

@JoshLove-msft JoshLove-msft commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Upgrades the C# emitter braces.cop cop rule to use the cop library's new structural braces support, and extends it to enforce braces on every control-flow body.

Changes

  • cop-checks/braces.cop — replaced the previous line-text regex heuristic with the code provider's structural parse (Statement.Braced / Statement.Children / isControlFlow). Flags any unbraced if / else / for / foreach / while body, whether written inline or on its own line (Allman). Idiomatic else if chains (an unbraced else whose child is an if) are excluded, while the inner if is still checked.
  • cop-checks/main.cop — wires the predicate to cb.Statements with an updated violation message.
  • eng/scripts/Invoke-Cop.ps1 — bumped the pinned cop release v2026.6.10.1v2026.6.23.2 (structural braces support).
  • 179 existing violations fixed across 77 .cs files by adding braces via the Roslyn IDE0011 add-braces fixer (dotnet format style).

Validation

  • npm run cop reports cop checks passed. (0 violations).
  • dotnet build Microsoft.TypeSpec.Generator.sln -c Release succeeds with 0 warnings / 0 errors (StyleCop clean).

Replace the line-text regex heuristic in the C# emitter braces cop rule with
the code provider's structural parse (Statement.Braced, Children, isControlFlow).
This removes the prior false positives on multi-line conditions and recognizes
else if chains correctly. The rule preserves the existing policy: only inline
same-line control-flow bodies are reported; Allman next-line bodies remain
allowed. Bump the pinned cop release to v2026.6.23.2, where the structural
braces support is available and validated.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Jun 23, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@11062

commit: 0697e99

@github-actions

Copy link
Copy Markdown
Contributor

No changes needing a change description found.

Extend braces.cop to flag every unbraced control-flow body (inline and
Allman next-line), not just inline bodies. Add braces to the 179 existing
violations via the IDE0011 add-braces fixer.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@JoshLove-msft JoshLove-msft changed the title Upgrade braces.cop to use cop library structural braces support Enforce braces on all control-flow bodies in the C# emitter (cop) Jun 23, 2026
@JoshLove-msft JoshLove-msft enabled auto-merge June 23, 2026 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants