Skip to content

SA1121: Preserve native integer compatibility in crefs and linked targets - #4395

Merged
AArnott merged 1 commit into
masterfrom
aarnott-silver-umbrella
Nov 3, 2026
Merged

AArnott merged 1 commit into
masterfrom
aarnott-silver-umbrella

Conversation

@AArnott

@AArnott AArnott commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

SA1121 could produce invalid native-integer replacements in XML documentation and in shared source compiled for multiple targets. This was reproduced with C# 14 and StyleCop.Analyzers 1.3.151-beta on microsoft/vs-threading commit c01683d2bf3a79e0de9c8cdce1476cb673357782.

Changes

  • Exempt IntPtr and UIntPtr in XML documentation cref attributes, where nint and nuint are not recognized. Other built-in type aliases in documentation still report and fix normally.
  • Before offering an individual fix or applying Fix All, check linked projects for native-integer support. Preserve framework type names if any linked target does not support C# 11 with .NET 7 or later.
  • Cover member access, pointer constructors, compatible and incompatible linked projects, solution-wide fixing, older language/runtime combinations, and the reported SynchronizationContext.Wait(IntPtr[], bool, int) reference.
  • Update the English, Czech, Russian, and Simplified Chinese rule documentation.

Multi-targeting behavior

The original member-access and constructor diagnostics come from the .NET 8 targets. Applying those replacements to the shared file breaks the netstandard2.0 target with CS0117 (nint.Size) and CS1729 (new nint(...)).

The analyzer still reports those locations in newer-target compilations because it cannot see other project compilations. The code fix now withholds unsafe replacements; users can suppress the remaining diagnostic where the framework type name is needed for compatibility.

Validation

  • The cref regression failed on the unchanged analyzer with seven unwanted SA1121 diagnostics.
  • The incompatible-linked-project regression failed before the code-fix change with the exact unwanted nint.Size, new nint(...), and new nuint(...) replacements.
  • Built the supplied vs-threading commit with the suppression removed, then confirmed that the suggested edits produce CS0117 and CS1729 on netstandard2.0.
  • Built the C# 15 test project and its dependencies with zero warnings.
  • All SA1121 tests passed in the C# 6 baseline and C# 9 through C# 15 projects.
  • Documentation source localization checks passed. Full documentation integration checks require generated DocFX assets absent from this worktree.

Performance

The analyzer adds an ancestor check only for supported IntPtr/UIntPtr identifiers, before semantic analysis; other identifier paths are unchanged. Linked-project compilation checks run only in code fixes for native-integer diagnostics in linked files. No benchmark was run.

…gets

Exclude IntPtr and UIntPtr from cref alias diagnostics and withhold individual and bulk replacements when a linked target lacks native integer alias support. Add regression coverage for shared source, runtime and language gates, and documentation references.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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.

🟢 Approval recommended

No active review issues are reported for the SA1121 compatibility changes.

No open issues 🎉

What changed in this PR

This PR updates SA1121 to avoid native-integer replacements that break XML documentation references or shared source compiled for older targets.

Changes:

  • Exempts IntPtr and UIntPtr in documentation cref attributes.
  • Checks linked-project compatibility before offering individual fixes or applying Fix All.
  • Adds compatibility regression tests and updates rule documentation in four languages.
File Description
test/​StyleCop.Analyzers.Test.CSharp9/​ReadabilityRules/​SA1121CSharp9UnitTests.cs Tests preservation of native-sized integer types in C# 9.
test/​StyleCop.Analyzers.Test.CSharp11/​ReadabilityRules/​SA1121CSharp11UnitTests.cs Adds documentation-reference, linked-project, and language/runtime compatibility tests.
src/​StyleCop.Analyzers/​ReadabilityRules/​SA1121UseBuiltInTypeAlias.cs Exempts native-sized integer references inside cref attributes.
src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1121CodeFixProvider.cs Guards individual fixes and Fix All against incompatible linked targets.
documentation/​zh-cn/​SA1121.md Documents the exceptions and compatibility behavior in Simplified Chinese.
documentation/​SA1121.md Documents the exceptions and compatibility behavior in English.
documentation/​ru-ru/​SA1121.md Documents the exceptions and compatibility behavior in Russian.
documentation/​cs-cz/​SA1121.md Documents the exceptions and compatibility behavior in Czech.

Review effort: Balanced

@codecov

codecov Bot commented Nov 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.76636% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.64%. Comparing base (9d24854) to head (25a7461).

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #4395    +/-   ##
========================================
  Coverage   97.63%   97.64%            
========================================
  Files        1349     1349            
  Lines      152904   153329   +425     
  Branches     5267     5272     +5     
========================================
+ Hits       149295   149719   +424     
  Misses       2280     2280            
- Partials     1329     1330     +1     
🚀 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 November 3, 2026 17:46
@AArnott
AArnott merged commit d4fdfe5 into master Nov 3, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-silver-umbrella branch November 3, 2026 17:46
This was referenced Nov 11, 2026
This was referenced Nov 13, 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