Skip to content

C# 13: handle partial properties and indexers in documentation rules - #4148

Merged
AArnott merged 7 commits into
masterfrom
csharp13-partial-properties
Oct 8, 2026
Merged

AArnott merged 7 commits into
masterfrom
csharp13-partial-properties

Conversation

@AArnott

@AArnott AArnott commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Part of #4045 (C# 13 audit), checklist item #4021 (partial properties and indexers).

Ported from @bjornhellander's 76c10906b (bjornhellander/NewStyleCopAnalyzers#99), cherry-picked with -x and his authorship kept.

Bugs: before this, C# 13 partial properties and indexers got none of the "partial element" treatment:

  • SA1601 (partial elements must be documented) never reported them.
  • SA1605/SA1607 (partial element summary / summary text) didn't check them. For a partial property, the <content> tag wasn't accepted in place of <summary>, which is what these rules allow for partial types and methods.
  • SA1604/SA1606 (element summary / summary text) handled them as ordinary properties.

Fix:

  • PartialElementDocumentationSummaryBase (SA1605/SA1607) and SA1601 now register for PropertyDeclaration and IndexerDeclaration and handle members with the partial modifier. The declaring part (accessors with no bodies) is treated like a partial method definition, so <content> is accepted there.
  • ElementDocumentationSummaryBase (SA1604/SA1606) now skips partial properties and indexers, the same way it already skips other partial elements.
  • Docs: SA1601.md, SA1605.md and SA1607.md list properties and indexers.
  • SA1601: one documented part is enough for a partial member (methods, properties and indexers). The compiler uses the implementing part's docs when present and otherwise the declaring part's, so an undocumented part is no longer reported when another part of the same member is documented. The other part comes from PartialDefinitionPart/PartialImplementationPart: directly on IMethodSymbol (available since Roslyn 1.2.1), and on IPropertySymbol through a new light-up accessor (IPropertySymbolExtensions, a static delegate created once, so no per-node reflection). The semantic lookup only runs after the current part is found to be undocumented, so documented members cost nothing extra. Partial types keep their current behavior (every part must be documented); SA1601: consider requiring documentation on only one part of a partial type #4176 tracks whether they should follow.
  • When no part is documented, each undocumented part is still reported, as before. I didn't pick just one part because the other part is often generated code (for example a source generator's implementing part), where diagnostics are suppressed. Reporting every undocumented part means your own part always gets the warning, and documenting any one part clears all of them.
  • SA1605/SA1607 are unchanged and stay consistent: they only check parts that have a documentation header, and an undocumented part is left to SA1601. Empty documentation (a whitespace-only <summary>) doesn't count as documenting the member, same as for a single part.
  • SA1601.md and the rule's XML docs describe the one-part rule.
  • Roslyn 1.x limitation: the Roslyn 1.x analyzer driver (used by the C# 6 test project, Roslyn 1.3.2) doesn't run syntax node actions on the implementing declaration of a partial method, so on Roslyn 1.x only the defining declaration is ever reported. That was already true before this PR; the two "no part documented" partial-method tests check LightupHelpers.SupportsCSharp7 for that.

Behavior changes

  • Fewer SA1601 diagnostics on partial methods (all language versions): an undocumented part of a partial method is no longer reported when its other part is documented. Before, each undocumented part was reported. The same applies to the new partial property/indexer support.
  • New SA1601 diagnostics on undocumented partial properties and indexers.
  • SA1604/SA1606 → SA1605/SA1607 for partial properties and indexers. Partial members only exist in C# 13+, so this doesn't affect older code.
  • SA1600 still reports undocumented partial properties too, so they currently get both SA1600 and SA1601. C# 13: SA1600 leaves partial methods, properties and indexers to SA1601 (stacked on #4148) #4149 (stacked on this PR) removes the SA1600 report.

Tests

  • SA1601 one-part rule: partial methods (base project) with only the declaring part documented, only the implementing part documented, neither (both reported), and an empty-docs implementing part (both reported). Partial properties and indexers (C# 13) with only the declaring part documented, only the implementing part documented, and neither. The two existing C# 13 tests that expected SA1601 on an undocumented declaring part whose implementing part is documented now expect none (renamed to ...DefinitionWithoutDocumentationAsync); that's the intended change above.
    New C# 13 tests ([WorkItem(4021)]) for SA1600, SA1601, SA1604, SA1605, SA1606, SA1607 (including <include> documentation and expression-bodied / block-bodied implementing parts), SA1201, SA1202, SA1205 and SA1206. All tests for these rules pass locally on C# 12, 13 and 15.

Adaptations: the fork also registers for C# 15 union declarations; that's dropped here, since upstream doesn't have those registrations. Upstream's SA1205 registration is unchanged. Tests use ConfigureAwait(false) and upstream's SA1201 message arguments. Björn left a few // TODO: notes in the code; I kept them as written for review.

Conflicts: this PR and #4142 both change the registration block in SA1601PartialElementsMustBeDocumented.cs, so whichever merges second needs a trivial merge.

…in some cases

From bjornhellander/NewStyleCopAnalyzers#99.

Adapted while cherry-picking: dropped the fork's review notes file and the 'union' registrations, added the
property and indexer registrations to the upstream compilation start actions, and kept upstream's SA1205
registration. Tests were merged into the existing upstream test classes and use ConfigureAwait(false),
WorkItem(4021) and the upstream SA1201 message arguments.

(cherry picked from commit 76c10906b2817e8fceee873c38a2864781b1eb8a)
@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.77089% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.51%. Comparing base (e3dc2d8) to head (87c388c).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4148      +/-   ##
==========================================
+ Coverage   97.48%   97.51%   +0.02%     
==========================================
  Files        1214     1223       +9     
  Lines      128311   130642    +2331     
  Branches     4209     4238      +29     
==========================================
+ Hits       125087   127391    +2304     
- Misses       2112     2126      +14     
- Partials     1112     1125      +13     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AArnott
AArnott marked this pull request as ready for review October 8, 2026 05:22
Comment on lines +50 to +74
public async Task TestPartialPropertyWithoutDocumentationAsync()
{
var testCode = @"
/// <summary>
/// Some Documentation
/// </summary>
public partial class TypeName
{
public partial int [|MemberName|] { get; set; }
}

/// <summary>
/// Some Documentation
/// </summary>
public partial class TypeName
{
/// <summary>
/// Some Documentation
/// </summary>
public partial int MemberName
{
get => 0;
set { }
}
}";

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test for the opposite, where the less-defined partial declaration has the docs and the more completely defined partial does not?
IMO it's crazy that both partials would be expected to have docs. Keeping them both, and in sync, seems onerous. IMO having either one documented should be adequate. And if both are documented, we should probably have an analyzer call out when they don't match exactly between xml elements that they both provide.

@AArnott AArnott Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: documenting either part is now enough for SA1601 on partial methods, properties and indexers (6ea9c6a, which adds the opposite-direction tests for all three), and on constructors and events in #4152 (f012d3d). #4149 matches this: an undocumented part gets no SA1600, and gets SA1601 only when no part is documented (tests in 532209b). Partial types are unchanged for now, and #4176 tracks them.

… and indexers

An undocumented part of a partial member is no longer reported when another part of the
same member is documented. The compiler uses the implementing declaration's documentation
when present, and otherwise the defining declaration's. When no part is documented, each
undocumented part is still reported. Partial types are unchanged.

The other part is only looked up (via IMethodSymbol.PartialDefinitionPart /
PartialImplementationPart, and the same properties on IPropertySymbol through a cached
light-up accessor) after the current part is found to be undocumented.
AArnott added a commit that referenced this pull request Oct 8, 2026
…ents

Extends the #4148 change to C# 14 partial constructors and events. The other part of an event
is found through a cached light-up accessor for IEventSymbol.PartialDefinitionPart /
PartialImplementationPart; constructors use IMethodSymbol's.
# Conflicts:
#	StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp13/DocumentationRules/SA1600CSharp13UnitTests.cs
#	StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp13/OrderingRules/SA1201CSharp13UnitTests.cs
#	StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp13/OrderingRules/SA1202CSharp13UnitTests.cs
AArnott added a commit that referenced this pull request Oct 8, 2026
…ents

Extends the #4148 change to C# 14 partial constructors and events. The other part of an event
is found through a cached light-up accessor for IEventSymbol.PartialDefinitionPart /
PartialImplementationPart; constructors use IMethodSymbol's.
The C# 6 test project runs on Roslyn 1.3.2, whose analyzer driver does not run syntax node
actions on the implementing declaration of a partial method, so only the defining declaration
is reported there. Roslyn 2.0+ (C# 7 and later test projects) reports both parts.
AArnott added a commit that referenced this pull request Oct 8, 2026
…ents

Extends the #4148 change to C# 14 partial constructors and events. The other part of an event
is found through a cached light-up accessor for IEventSymbol.PartialDefinitionPart /
PartialImplementationPart; constructors use IMethodSymbol's.
AArnott added a commit that referenced this pull request Oct 8, 2026
…ents

Extends the #4148 change to C# 14 partial constructors and events. The other part of an event
is found through a cached light-up accessor for IEventSymbol.PartialDefinitionPart /
PartialImplementationPart; constructors use IMethodSymbol's.
AArnott added a commit that referenced this pull request Oct 8, 2026
…ents

Extends the #4148 change to C# 14 partial constructors and events. The other part of an event
is found through a cached light-up accessor for IEventSymbol.PartialDefinitionPart /
PartialImplementationPart; constructors use IMethodSymbol's.
@AArnott
AArnott merged commit 41c091f into master Oct 8, 2026
25 checks passed
@AArnott
AArnott deleted the csharp13-partial-properties branch October 8, 2026 19:47
AArnott added a commit that referenced this pull request Oct 8, 2026
…ed part

An undocumented part of a partial method, property or indexer whose other part is documented
gets neither SA1600 (partial members are left to SA1601) nor SA1601 (one documented part is
enough, #4148).
This was referenced Nov 24, 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