Skip to content

Fix SA1649 for C# 15 union declarations and C# 14 extension blocks - #4127

Merged
AArnott merged 3 commits into
masterfrom
sa1649-union-declarations
Oct 7, 2026
Merged

AArnott merged 3 commits into
masterfrom
sa1649-union-declarations

Conversation

@AArnott

@AArnott AArnott commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Supersedes #4096. Thanks to @verdie-g for the original fix, which this PR builds on unchanged: his commit b2ebf76 is included with its original SHA and authorship.

The problem

On master, SA1649 crashes with AD0001 (ArgumentException: Unhandled declaration kind) when the first type in a file is a declaration kind that NamedTypeHelpers.GetNameOrIdentifier doesn't list explicitly. Two cases hit this:

Commits

  1. b2ebf76 Fix SA1649 for union declarations (@verdie-g, from Fix SA1649 for C# 15 union declarations #4096, unchanged).
    • GetNameOrIdentifier now handles every TypeDeclarationSyntax, so new type kinds like union work.
    • Adds union tests to SA1649CSharp15UnitTests.
  2. 8c3dff1 Signed merge of origin/master.
    • Conflicts were resolved in favor of the real C# 15 test project from Add a C# 15 preview test project #4125, so the ad-hoc C# 15 scaffolding in b2ebf76 is dropped. That covers the project file, InternalsVisibleTo entries, the solution entry, the GenericAnalyzerTest mapping, and the Azure Pipelines YAML, which master has since deleted.
    • SA1649CSharp15UnitTests keeps both union tests. It now derives from SA1649CSharp14UnitTests and has a UTF-8 BOM (required by SA1412).
  3. 9d88651 SA1649: ignore type declarations without a name.
    • An extension block is a TypeDeclarationSyntax with no identifier. With commit 1 alone, a top-level extension block no longer crashed, but SA1649 reported a diagnostic with no source location and an empty expected file name.
    • SA1649 now skips type declarations without an identifier when it picks the type the file name must match.
    • Adds C# 14 tests.

Relative to master, the whole PR changes only NamedTypeHelpers.cs, SA1649FileNameMustMatchTypeName.cs, and the SA1649 C# 14/15 test classes.

Behavior change

Code master this PR
public union TestUnion(int, string); in WrongFileName.cs (C# 15) AD0001 crash, no SA1649 SA1649 on TestUnion; the code fix renames the file to TestUnion.cs
public union TestUnion<T>(T, string); in TestUnion.cs AD0001 crash no diagnostic
Top-level extension(object o) { } alone in WrongFileName.cs (C# 14, doesn't compile) AD0001 crash no diagnostic (no type name to match)
Top-level extension block followed by public class TestClass in WrongFileName.cs AD0001 crash SA1649 on TestClass; the code fix renames the file to TestClass.cs
public static class TestExtensions { extension(object o) { ... } } in WrongFileName.cs SA1649 on TestExtensions unchanged

Named type declarations (classes, structs, interfaces, records, enums, delegates) behave exactly as before.

Testing

  • Locally (net6.0): SA1649 and SA1402 tests pass in C# 14 (237) and C# 15 (239).
  • Without the commit-3 change, both new top-level extension-block tests fail.

verdie-g and others added 3 commits August 29, 2026 18:02
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
…ations

Conflicts were resolved in favor of master's C# 15 test project (#4125), so
the ad-hoc C# 15 scaffolding from b2ebf76 is dropped: the project file,
InternalsVisibleTo entries, solution entry, GenericAnalyzerTest version mapping
and the Azure Pipelines changes (build/*.yml no longer exist on master).

SA1649CSharp15UnitTests keeps the new union tests, now derives from
SA1649CSharp14UnitTests (the C# 15 project's base) and has a UTF-8 BOM like the
other test sources (SA1412).
A C# 14 extension block is a TypeDeclarationSyntax whose Identifier is a
default (None) token. When one appears at the top level of a file (a compile
error, but common while typing), SA1649 picked it as the file's first type:
before b2ebf76 this crashed with AD0001 ("Unhandled declaration kind:
ExtensionBlockDeclaration"); with b2ebf76 it reported SA1649 with no source
location and an empty expected file name.

SA1649 now skips type declarations that have no identifier when choosing the
type the file name must match, so it either checks the next named type in the
file or reports nothing.

Adds C# 14 tests for an extension block inside a static class (unchanged
behavior), a lone top-level extension block (no diagnostic), and a top-level
extension block before a class (the class is checked).
@AArnott
AArnott marked this pull request as ready for review October 7, 2026 23:34
@AArnott
AArnott merged commit 12d7949 into master Oct 7, 2026
23 checks passed
@AArnott
AArnott deleted the sa1649-union-declarations branch October 7, 2026 23:40
@verdie-g

verdie-g commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Thx for reviving this project

This was referenced Nov 11, 2026
This was referenced Nov 20, 2026
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.

2 participants