Skip to content

Require documentation for C# 14 extension members (extension blocks no longer treated as private) - #4163

Merged
AArnott merged 1 commit into
masterfrom
csharp14-extension-member-accessibility
Oct 8, 2026
Merged

AArnott merged 1 commit into
masterfrom
csharp14-extension-member-accessibility

Conversation

@AArnott

@AArnott AArnott commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Part of #4046 (C# 14 audit), checklist item #4023 (extension members). Independent master-based PR.

Bug: AccessLevelHelper.GetDeclaredAccessibility(BaseTypeDeclarationSyntax) treated a C# 14 extension(...) { } block like a nested type with no modifiers, i.e. private. So every member of an extension block had an effective accessibility of private, and SA1600 and the other documentation rules that use NeedsComment never required documentation on public extension members such as public int Length => source.Length; in a public static class.

Fix: an extension block now reports Accessibility.Public as its declared accessibility. The block doesn't restrict its members, so their effective accessibility is their own modifier combined with the containing static class's accessibility. The extension block itself still doesn't need documentation, because SA1600 doesn't register for ExtensionBlockDeclaration. That matches @bjornhellander's fork (bjornhellander/NewStyleCopAnalyzers#30) test TestExtensionBlockDeclarationAsync. Only the documentation rules call these helpers. SA1202 uses its own MemberOrderHelper access levels and is unaffected.

Behavior change (please review)

  • New SA1600 diagnostics on undocumented public/internal members of extension blocks (honoring documentInternalElements/documentPrivateElements as usual). Before this, they were silently treated as private.
  • Other documentation rules that gate on NeedsComment (for example SA1611/SA1615/SA1618 when documentation is required) now treat extension members as exposed.

Tests

C# 14 ([WorkItem(4023)]): in a public static class, public, static and internal extension members are reported and private ones aren't. In an internal static class, a public member is reported (internal effective accessibility, default documentInternalElements: true).

Local: all DocumentationRules tests pass on C# 14 apart from the known Linux CRLF code-fix failures.

Not in this PR (decision for Andrew): ordering rules (SA1201/SA1202/SA1204) still don't look inside extension blocks, e.g. private before public members, or instance before static extension members.

Members of an extension block are exposed through the containing static class, but the block has no modifiers and was treated as a private nested type. As a result, SA1600 and the other documentation rules treated every extension member as private and never required documentation for them.

Part of #4046 (checklist item #4023).
@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.39%. Comparing base (53ce0bf) to head (fd02dfb).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4163   +/-   ##
=======================================
  Coverage   97.39%   97.39%           
=======================================
  Files        1073     1074    +1     
  Lines      122990   123037   +47     
  Branches     4173     4174    +1     
=======================================
+ Hits       119781   119828   +47     
  Misses       2108     2108           
  Partials     1101     1101           
🚀 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 06:01
@AArnott
AArnott merged commit f26d21b into master Oct 8, 2026
25 checks passed
@AArnott
AArnott deleted the csharp14-extension-member-accessibility branch October 8, 2026 14:55
This was referenced Nov 11, 2026
This was referenced Nov 23, 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.

1 participant