Skip to content

Support C# 15 union declarations in SA1201, SA1202 and SA1402 - #4188

Merged
AArnott merged 2 commits into
masterfrom
csharp15-union-declarations-ordering-single-type
Oct 8, 2026
Merged

AArnott merged 2 commits into
masterfrom
csharp15-union-declarations-ordering-single-type

Conversation

@AArnott

@AArnott AArnott commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Part of #4180 (C# 15 audit): #4182 (union types).

A C# 15 union declaration declares a struct, but SA1201, SA1202 and SA1402 didn't know about UnionDeclaration syntax:

Rule Before After
SA1201 ❌ unions weren't in the element order, so anything before a union was reported, as "<unknown> should not follow ..." ✅ unions ordered like structs; messages say "a union"
SA1202 ❌ unions skipped ✅ unions ordered by access, compared with other unions (like record structs)
SA1402 ❌ unions not counted, even with struct in topLevelTypes ✅ counted as structs
SA1402 code fix ❌ copied unions into the file extracted for another type ✅ unions stay in the original file

Change

  • SyntaxKindEx adds UnionDeclaration (9082). SyntaxKindExUnitTests checks it against Roslyn.
  • SA1201 and MemberOrderHelper (used by the ordering code fix) map UnionDeclaration to StructDeclaration. SA1201 names it "a union".
  • SA1202 adds UnionDeclaration to the member kinds it checks.
  • SA1402 treats unions as structs, and its code fix removes them from the extracted document.

Tests

New C# 15 tests in SA1201CSharp15UnitTests, SA1202CSharp15UnitTests and SA1402ForStructCSharp15UnitTests, at namespace level and nested in a class, with code fixes. They use LanguageVersion.Preview (Roslyn 5.9 has no LanguageVersion.CSharp15 yet, so union syntax only parses with Preview) and CompilerDiagnostics.None, because the reference assemblies don't define IUnion and UnionAttribute yet.

Not included

Rules that analyze union members (or the union node itself through a syntax node action, such as SA1600/SA1300/SA1400/SA1206) aren't changed here. The Roslyn 5.9 analyzer driver runs syntax node callbacks for a union declaration twice and for its members three times, so those diagnostics would come out duplicated. That's tracked on #4182. These tests only report from compilation unit, namespace and enclosing type callbacks, which run once.

Behavior changes

None for existing code. Unions are new C# 15 preview syntax.

SA1201/SA1202/SA1402 C# 15 tests pass locally (Linux), apart from the known Linux-only CRLF code-fix baseline failures; the new tests' only diffs are those CRLF lines. SyntaxKindEx tests pass in the base project.

A union declaration declares a struct, so:

* SA1201 orders unions like structs and names them "a union" in its
  message (it used to report "<unknown>" and order them like methods).
* SA1202 orders unions by access, compared with other unions (like record
  structs, which are compared with other record structs).
* SA1402 counts unions as structs for the topLevelTypes setting, and its
  code fix no longer copies unions into the file it extracts for another
  type.

Part of #4180 (#4182).
@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.51%. Comparing base (6536231) to head (91e42e3).

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #4188    +/-   ##
========================================
  Coverage   97.51%   97.51%            
========================================
  Files        1236     1239     +3     
  Lines      132342   132573   +231     
  Branches     4284     4286     +2     
========================================
+ Hits       129048   129279   +231     
  Misses       2155     2155            
  Partials     1139     1139            
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AArnott
AArnott marked this pull request as ready for review October 8, 2026 21:17
@AArnott
AArnott merged commit be89c05 into master Oct 8, 2026
25 checks passed
@AArnott
AArnott deleted the csharp15-union-declarations-ordering-single-type branch October 8, 2026 23:39
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.

1 participant