Repository navigation
SA1121: Preserve native integer compatibility in crefs and linked targets - #4395
Merged
Merged
Conversation
…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>
There was a problem hiding this comment.
🟢 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
IntPtrandUIntPtrin documentationcrefattributes. - 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 Report❌ Patch coverage is 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:
|
AArnott
marked this pull request as ready for review
November 3, 2026 17:46
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
This was referenced Nov 13, 2026
Open
Open
Merged
Open
Bump StyleCop.Analyzers from 1.2.0-beta.556 to 1.3.180
microsoftgraph/msgraph-sample-aspnet-core#256
Open
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.
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
IntPtrandUIntPtrin XML documentationcrefattributes, wherenintandnuintare not recognized. Other built-in type aliases in documentation still report and fix normally.SynchronizationContext.Wait(IntPtr[], bool, int)reference.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
nint.Size,new nint(...), andnew nuint(...)replacements.Performance
The analyzer adds an ancestor check only for supported
IntPtr/UIntPtridentifiers, 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.