Repository navigation
Support C# 15 union declarations in SA1201, SA1202 and SA1402 - #4188
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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:
|
…arations-ordering-single-type
AArnott
marked this pull request as ready for review
October 8, 2026 21:17
This was referenced Oct 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #4180 (C# 15 audit): #4182 (union types).
A C# 15
uniondeclaration declares a struct, but SA1201, SA1202 and SA1402 didn't know aboutUnionDeclarationsyntax:structintopLevelTypesChange
SyntaxKindExaddsUnionDeclaration(9082).SyntaxKindExUnitTestschecks it against Roslyn.MemberOrderHelper(used by the ordering code fix) mapUnionDeclarationtoStructDeclaration. SA1201 names it "a union".UnionDeclarationto the member kinds it checks.Tests
New C# 15 tests in
SA1201CSharp15UnitTests,SA1202CSharp15UnitTestsandSA1402ForStructCSharp15UnitTests, at namespace level and nested in a class, with code fixes. They useLanguageVersion.Preview(Roslyn 5.9 has noLanguageVersion.CSharp15yet, so union syntax only parses with Preview) andCompilerDiagnostics.None, because the reference assemblies don't defineIUnionandUnionAttributeyet.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.