You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SA1121: Add multiTargetsLegacyFrameworks option and code fix - #4443
SA1121 reports IntPtr/UIntPtr in shared files of multi-targeted projects even when an older target cannot use nint/nuint. An analyzer only sees one target's compilation, so it cannot detect this itself.
Adds compatibility.multiTargetsLegacyFrameworks (stylecop.json and stylecop.compatibility.multiTargetsLegacyFrameworks in .editorconfig/.globalconfig). When true, SA1121 no longer reports IntPtr/UIntPtr. The name is general so other rules can reuse it.
The code fix can see linked projects and already withholds its fix in that situation. A new provider, SA1121MultiTargetsLegacyFrameworksCodeFixProvider, then offers to set the option in an existing stylecop.json, else the nearest .editorconfig, else a .globalconfig. It never creates files and has no Fix All.
Editing analyzer config documents needs light-up (Project.AnalyzerConfigDocuments, Solution.WithAnalyzerConfigDocumentText), since the code fixes assembly compiles against Roslyn 1.2.1.
Schema, localized resources (ru-RU, zh-CN) and docs (en, cs, ru, zh) are updated.
Tests: SA1121 (C# 11), settings, and new SettingsFileEditor unit tests pass. The .editorconfig/.globalconfig code fix paths are covered only by the editor unit tests, not end to end.
Adds compatibility.multiTargetsLegacyFrameworks, which stops SA1121 from reporting IntPtr/UIntPtr. When the SA1121 fix is withheld because a linked project lacks native-sized integers, a new code action sets the option in an existing stylecop.json or .editorconfig/.globalconfig.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The fix can insert duplicate settings keys when JSON property names contain escapes, causing the settings reader to reject valid configuration and discard existing settings.
Adds a compatibility setting for SA1121 so projects targeting both modern and legacy frameworks can retain IntPtr and UIntPtr without disabling checks for other built-in types, addressing #4440.
Changes:
Adds compatibility.multiTargetsLegacyFrameworks with JSON and analyzer-config support.
Introduces a code action that updates existing configuration files, backed by Roslyn light-up helpers.
Adds tests, schema entries, localized resources, and documentation in four languages.
❌ Patch coverage is 80.22690% with 122 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.44%. Comparing base (bf1dfa1) to head (b2c81d1). ⚠️ Report is 4 commits behind head on master.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The compatibility option incorrectly suppresses SA1121 diagnostics for other built-in types behind misleading aliases, and the light-up extension bypasses the required reflection boundary.
No open issues 🎉
2 previously missed issues
In code that hasn't changed since last review
Limit alias suppression to actual IntPtr and UIntPtr types
With both allowBuiltInTypeAliases and multiTargetsLegacyFrameworks enabled on a native-integer-capable target, using IntPtr = System.Int32; now stops reporting SA1121 on System.Int32. The compatibility option should suppress references to the actual IntPtr/UIntPtr types, not diagnostics for other built-in types hidden behind misleading aliases. Keep the compilation capability flag for this alias-name check.
The hand-written light-up extension performs reflection directly here. The light-up guidance requires reflection to remain in LightupHelpers, with extension classes using cached accessors. Move the type lookup into LightupHelpers (or avoid it if the type is available in the baseline API) so this helper preserves that boundary.
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
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 reports
IntPtr/UIntPtrin shared files of multi-targeted projects even when an older target cannot usenint/nuint. An analyzer only sees one target's compilation, so it cannot detect this itself.compatibility.multiTargetsLegacyFrameworks(stylecop.json andstylecop.compatibility.multiTargetsLegacyFrameworksin .editorconfig/.globalconfig). When true, SA1121 no longer reportsIntPtr/UIntPtr. The name is general so other rules can reuse it.SA1121MultiTargetsLegacyFrameworksCodeFixProvider, then offers to set the option in an existing stylecop.json, else the nearest .editorconfig, else a .globalconfig. It never creates files and has no Fix All.Project.AnalyzerConfigDocuments,Solution.WithAnalyzerConfigDocumentText), since the code fixes assembly compiles against Roslyn 1.2.1.Tests: SA1121 (C# 11), settings, and new
SettingsFileEditorunit tests pass. The .editorconfig/.globalconfig code fix paths are covered only by the editor unit tests, not end to end.Relates to #4440