Skip to content

Fix NRE in using code fix when qualifying alias-any-type usings - #4381

Merged
AArnott merged 6 commits into
masterfrom
aarnott-fuzz-using-alias-any-type-nre
Oct 25, 2026
Merged

AArnott merged 6 commits into
masterfrom
aarnott-fuzz-using-alias-any-type-nre

Conversation

@AArnott

@AArnott AArnott commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Found by the fuzz harness (tools/StyleCop.Analyzers.Fuzz, mutate over the extracted-literal corpus, random seed 2026).

Bug: the SA1200 code fix (UsingCodeFixProvider) throws a NullReferenceException in QualifyUsingDirective when it moves a C# 12 alias-any-type using (e.g. using Point = (int X, int Y);, using Numbers = int[];) and has to qualify it. For these aliases, UsingDirectiveSyntax.Name is null.

Fix: when Name is null, qualify each outermost NameSyntax inside the alias target type (NamespaceOrType()). The existing symbol-qualification logic is extracted into QualifyName and reused unchanged.

Test: SA1200OutsideNamespaceCSharp12UnitTests.TestAliasAnyTypeInNamespaceIsMovedAndQualifiedAsync. It failed with the NRE before the fix.

Analyzer cost: none. This is code-fix only.

Known limitations

  • A named alias declared outside the namespace whose target uses an extern alias in a type argument (e.g. extern alias ext; using Box = global::G<ext::Lib.C>; with using Items = Box[]; inside the namespace) is qualified as G<Lib.C>[], losing the ext:: qualifier.
  • An alias declared in another file through an extern alias (e.g. extern alias ext; global using Rows = ext::Lib.C[];) is expanded to global::Lib.C[][] when using Items = Rows[]; is moved, also losing the ext:: qualifier.

Both only matter when the referenced library is reachable solely through the extern alias.

@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.15789% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.63%. Comparing base (b0ba8d8) to head (6e2de21).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4381      +/-   ##
==========================================
- Coverage   97.63%   97.63%   -0.01%     
==========================================
  Files        1345     1346       +1     
  Lines      151867   152300     +433     
  Branches     5219     5238      +19     
==========================================
+ Hits       148275   148696     +421     
- Misses       2268     2274       +6     
- Partials     1324     1330       +6     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@AArnott
AArnott marked this pull request as ready for review October 25, 2026 14:39
Copilot AI balanced review requested due to automatic review settings October 25, 2026 14:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Remaining qualification cases can leave names unresolved or lose tuple element names, producing code that no longer compiles.

3 open issues
What changed in this PR

This PR addresses an SA1200 code-fix crash when moving C# 12 alias-any-type using directives outside a namespace.

Changes:

  • Handles alias targets whose legacy Name property is null.
  • Extracts name qualification into a shared helper and applies it within composite type targets.
  • Adds a regression test covering tuple and array aliases.
File Description
test/​StyleCop.Analyzers.Test.CSharp12/​OrderingRules/​SA1200OutsideNamespaceCSharp12UnitTests.cs Tests moving and qualifying tuple and array aliases.
src/​StyleCop.Analyzers.CodeFixes/​OrderingRules/​UsingCodeFixProvider.UsingsSorter.cs Adds composite-target qualification and extracts shared qualification logic.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 25, 2026 14:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Alias rewriting can change array nesting or nullability and break extern-alias references after relocation.

3 open issues
3 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 25, 2026 15:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new rewrite can leave enclosing generic types unresolved, remove nullable-reference annotations, and discard internal comments.

3 open issues
3 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 25, 2026 15:29
…alias-any-type-nre

# Conflicts:
#	src/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.UsingsSorter.cs

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Remaining alias-scope, nested-generic, and nullable-handling issues can produce uncompilable code or alter nullability.

2 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Preserve nullable annotations during cross-tree alias expansion

src/​StyleCop.Analyzers.CodeFixes/​OrderingRules/​UsingCodeFixProvider.UsingsSorter.cs:512

The cross-tree fallback drops nullable-reference annotations because FullyQualifiedFormat omits those modifiers. With annotations enabled, global using Row = N.C?[]; in another file and a namespace-local using Matrix = Row[]; produce N.C[][] instead of N.C?[][], changing element nullability. Use a nullable-aware display format compatible with the supported Roslyn baseline and add a multi-file regression test.

Medium severity Preserve nullable context when relocating aliases

test/​StyleCop.Analyzers.Test.CSharp12/​OrderingRules/​SA1200OutsideNamespaceCSharp12UnitTests.cs:317

This expected output moves TestClass?[] before #nullable enable, outside the annotation context that covered the original alias. When the project's default annotation context is disabled, the fix introduces CS8632 and can break builds that treat warnings as errors. Preserve the alias's nullable context during relocation and make this regression check compiler warnings as well as the fixed text.

3 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 25, 2026 15:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The code fix can still discard comments and nullable annotations or introduce compiler warnings by changing nullable context.

5 open issues

Review effort: Balanced

… targets

A using alias qualifier (NS::C) is not visible from another alias target in the same
using block, so only extern aliases are preserved. Type arguments are now preserved on
every generic segment, e.g. Outer<ext::C>.Inner<int>.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AArnott
AArnott merged commit 6449f6c into master Oct 25, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-fuzz-using-alias-any-type-nre branch October 25, 2026 16:48
This was referenced Nov 24, 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