Skip to content

SA1121: Add multiTargetsLegacyFrameworks option and code fix - #4443

Merged
AArnott merged 3 commits into
masterfrom
aarnott-automatic-train
Nov 23, 2026
Merged

AArnott merged 3 commits into
masterfrom
aarnott-automatic-train

Conversation

@AArnott

@AArnott AArnott commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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.

Relates to #4440

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>
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.

🟡 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.

5 open issues
1 file not reviewed
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/ReadabilityResources.Designer.cs: Generated file
What changed in this PR

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.
File Description
test/​StyleCop.Analyzers.Test/​Settings/​SettingsUnitTests.cs Checks the compatibility setting’s default.
test/​StyleCop.Analyzers.Test/​Settings/​SettingsFileEditorUnitTests.cs Tests configuration text editing.
test/​StyleCop.Analyzers.Test.CSharp8/​Settings/​SettingsCSharp8UnitTests.cs Tests EditorConfig support and JSON precedence.
test/​StyleCop.Analyzers.Test.CSharp11/​ReadabilityRules/​SA1121SettingsCodeFixCSharp11UnitTests.cs Tests the JSON configuration code action.
test/​StyleCop.Analyzers.Test.CSharp11/​ReadabilityRules/​SA1121CSharp11UnitTests.cs Tests native-integer diagnostic suppression.
StyleCop.Analyzers/​StyleCop.Analyzers/​Settings/​stylecop.schema.json Defines the compatibility option’s schema.
src/​StyleCop.Analyzers/​Settings/​ObjectModel/​StyleCopSettings.cs Integrates compatibility settings.
src/​StyleCop.Analyzers/​Settings/​ObjectModel/​CompatibilitySettings.cs Reads the new option through SettingsReader.
src/​StyleCop.Analyzers/​ReadabilityRules/​SA1121UseBuiltInTypeAlias.cs Applies the compatibility exemption.
src/​StyleCop.Analyzers/​ReadabilityRules/​ReadabilityResources.zh-CN.resx Adds the Chinese code-action title.
src/​StyleCop.Analyzers/​ReadabilityRules/​ReadabilityResources.ru-RU.resx Adds the Russian code-action title.
src/​StyleCop.Analyzers/​ReadabilityRules/​ReadabilityResources.resx Adds the English code-action title.
src/​StyleCop.Analyzers/​Lightup/​LightupHelpers.cs Supports three-argument method accessors.
src/​StyleCop.Analyzers/​Lightup/​.generated/​Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/​Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/​ReadabilityResources.Designer.cs Exposes the generated title accessor.
src/​StyleCop.Analyzers.CodeFixes/​Settings/​SettingsFileEditor.cs Edits JSON and analyzer-config settings.
src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1121MultiTargetsLegacyFrameworksCodeFixProvider.cs Offers the configuration-editing action.
src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1121CodeFixProvider.cs Shares the linked-target compatibility check.
src/​StyleCop.Analyzers.CodeFixes/​Lightup/​SolutionExtensions.cs Enables analyzer-config document updates.
src/​StyleCop.Analyzers.CodeFixes/​Lightup/​ProjectExtensions.cs Retrieves analyzer-config documents through light-up.
src/​StyleCop.Analyzers.CodeFixes/​Lightup/​AnalyzerConfigDocumentInfo.cs Holds analyzer-config document metadata and text.
documentation/​zh-cn/​SA1121.md Explains compatibility behavior in Chinese.
documentation/​zh-cn/​Configuration.md Documents the option in Chinese.
documentation/​SA1121.md Explains compatibility behavior in English.
documentation/​ru-ru/​SA1121.md Explains compatibility behavior in Russian.
documentation/​ru-ru/​Configuration.md Documents the option in Russian.
documentation/​cs-cz/​SA1121.md Explains compatibility behavior in Czech.
documentation/​cs-cz/​Configuration.md Documents the option in Czech.
documentation/​Configuration.md Documents the option and configuration mapping in English.

Review effort: Balanced

…nusable, test globalconfig

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

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ 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.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4443      +/-   ##
==========================================
- Coverage   97.50%   97.44%   -0.07%     
==========================================
  Files        1372     1379       +7     
  Lines      160132   160746     +614     
  Branches     5717     5812      +95     
==========================================
+ Hits       156140   156632     +492     
- Misses       2544     2614      +70     
- Partials     1448     1500      +52     
🚀 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 23, 2026 22:22
Copilot AI balanced review requested due to automatic review settings November 23, 2026 22:22
@AArnott
AArnott merged commit 78cb59b into master Nov 23, 2026
29 checks passed
@AArnott
AArnott deleted the aarnott-automatic-train branch November 23, 2026 22:23

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.

🔵 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

Medium severity Limit alias suppression to actual IntPtr and UIntPtr types

src/​StyleCop.Analyzers/​ReadabilityRules/​SA1121UseBuiltInTypeAlias.cs:291

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.

Low severity Move reflection lookup into LightupHelpers

src/​StyleCop.Analyzers.CodeFixes/​Lightup/​ProjectExtensions.cs:25

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.

5 resolved since last review
1 file not reviewed
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/ReadabilityResources.Designer.cs: Generated file

Review effort: Balanced

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