Repository navigation
Fix SA1649 for C# 15 union declarations and C# 14 extension blocks - #4127
Merged
Merged
Conversation
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
marked this pull request as ready for review
October 7, 2026 23:34
This was referenced Oct 7, 2026
Contributor
|
Thx for reviving this project |
This was referenced Nov 11, 2026
Bump StyleCop.Analyzers from 1.2.0-beta.556 to 1.3.180
microsoftgraph/aspnetcore-webhooks-sample#436
Open
Closed
Open
This was referenced Nov 20, 2026
Open
Open
Closed
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.
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 thatNamedTypeHelpers.GetNameOrIdentifierdoesn't list explicitly. Two cases hit this:uniondeclarations. This is the crash Fix SA1649 for C# 15 union declarations #4096 fixed.Commits
GetNameOrIdentifiernow handles everyTypeDeclarationSyntax, so new type kinds likeunionwork.SA1649CSharp15UnitTests.origin/master.GenericAnalyzerTestmapping, and the Azure Pipelines YAML, which master has since deleted.SA1649CSharp15UnitTestskeeps both union tests. It now derives fromSA1649CSharp14UnitTestsand has a UTF-8 BOM (required by SA1412).TypeDeclarationSyntaxwith 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.Relative to master, the whole PR changes only
NamedTypeHelpers.cs,SA1649FileNameMustMatchTypeName.cs, and the SA1649 C# 14/15 test classes.Behavior change
public union TestUnion(int, string);inWrongFileName.cs(C# 15)TestUnion; the code fix renames the file toTestUnion.cspublic union TestUnion<T>(T, string);inTestUnion.csextension(object o) { }alone inWrongFileName.cs(C# 14, doesn't compile)public class TestClassinWrongFileName.csTestClass; the code fix renames the file toTestClass.cspublic static class TestExtensions { extension(object o) { ... } }inWrongFileName.csTestExtensionsNamed type declarations (classes, structs, interfaces, records, enums, delegates) behave exactly as before.
Testing