Skip to content

SA1100: avoid unsafe virtual dispatch fixes - #4273

Merged
AArnott merged 1 commit into
masterfrom
aarnott-sa1100-dispatch
Oct 13, 2026
Merged

AArnott merged 1 commit into
masterfrom
aarnott-sa1100-dispatch

Conversation

@AArnott

@AArnott AArnott commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #2938

Changing base.DoSomething() to this.DoSomething() in an intermediate class can dispatch to a subclass override and recurse through the intermediate helper until stack overflow. The diagnostic is valid, but this change is not safe to apply automatically.

The analyzer is unchanged. The code fix now withholds both individual fixes and Fix All replacements for overridable members in inheritable receiver types. Non-virtual members, sealed overrides, and calls in sealed receiver types retain their automatic fixes. The same guard covers methods, properties, indexers, and events. English, Russian, and Czech rule documentation describe the limitation.

Regression evidence: TestSubclassOverrideCallsIntermediateHelperAsync was added before any product change and failed against master 5fb0ffaf999434bf5b78b7c8bb86c8334ac46b37. The verifier showed the exact unsafe diff: expected base.DoSomething(), actual this.DoSomething(). It now passes while retaining the SA1100 diagnostic. Existing virtual-call code-fix expectations now explicitly require unchanged source and the same diagnostics; none of these tests were skipped or disabled.

Additional coverage exercises inherited overrides, sealed members and receiver types, property/indexer getters and setters, event add/remove, and mixed Fix All operations with comments and an unsafe-only second document. Method-group cases verify the analyzer's existing behavior remains unchanged.

Validation with SDK 10.0.401:

  • dotnet build -c Release -maxcpucount:1 --verbosity quiet: succeeded, 0 warnings and 0 errors.
  • Release SA1100 suites on net10.0 in all C# 6–15 test projects: 662 passed, 0 failed, 0 skipped (63 in the lowest project; 68 in C# 15).
  • Debug lowest/latest SA1100 suites also passed during development (63/68).
  • git diff --check: clean.

Performance: no analyzer changes or analysis-time cost. Semantic safety checks run only in the code fix, reusing one semantic model per registration/document. Shipped Roslyn dependency pins are unchanged.

This PR remains a draft for CI and human review.

Keep the diagnostic, but withhold individual and Fix All changes for overridable members in inheritable types. Preserve safe fixes for sealed members and receiver types.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 13, 2026 15:48
Copilot AI balanced review requested due to automatic review settings October 13, 2026 15:48

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

The safety guard is correctly scoped to code fixes and has comprehensive regression coverage.

0 open findings

What changed in this PR

Prevents SA1100 code fixes from changing virtual dispatch while preserving the diagnostic.

Changes:

  • Adds semantic safety checks for individual and Fix All operations.
  • Expands regression coverage across member kinds and inheritance scenarios.
  • Documents the limitation in English, Russian, and Czech.
File Description
test/​StyleCop.Analyzers.Test/​ReadabilityRules/​SA1100UnitTests.cs Adds comprehensive safety and Fix All tests.
test/​StyleCop.Analyzers.Test.CSharp9/​ReadabilityRules/​SA1100CSharp9UnitTests.cs Updates covariant-override expectations.
src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1100CodeFixProvider.cs Withholds unsafe virtual-dispatch fixes.
documentation/​SA1100.md Documents code-fix limitations.
documentation/​ru-ru/​SA1100.md Adds Russian documentation.
documentation/​cs-cz/​SA1100.md Adds Czech documentation.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@AArnott
AArnott merged commit c43ceac into master Oct 13, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-sa1100-dispatch branch October 13, 2026 16:04
@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.08029% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.61%. Comparing base (5fb0ffa) to head (1660fe5).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4273      +/-   ##
==========================================
- Coverage   97.61%   97.61%   -0.01%     
==========================================
  Files        1285     1285              
  Lines      137307   137241      -66     
  Branches     4451     4467      +16     
==========================================
- Hits       134029   133962      -67     
  Misses       2113     2113              
- Partials     1165     1166       +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This was referenced Nov 11, 2026
This was referenced Nov 20, 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.

SA1100 incorrectly removes base. if a class doesn't have a local implementation but a subclass does

2 participants