Skip to content

Ignore primary constructors in MA0196 and reuse shared detection#1140

Merged
meziantou merged 1 commit into
mainfrom
meziantou/ignore-primary-ctors
May 8, 2026
Merged

Ignore primary constructors in MA0196 and reuse shared detection#1140
meziantou merged 1 commit into
mainfrom
meziantou/ignore-primary-ctors

Conversation

@meziantou

@meziantou meziantou commented May 8, 2026

Copy link
Copy Markdown
Owner

MA0196 should not flag <inheritdoc /> attached to primary constructor syntax, because that XML documentation primarily describes the containing type. This change fixes that false positive and consolidates primary-constructor detection logic.

What changed

  • Skip MA0196 diagnostics when the analyzed symbol is a primary constructor.
  • Add a shared IsPrimaryConstructor helper in SymbolExtensions.
  • Reuse the shared helper in existing primary-constructor detection paths:
    • InheritdocShouldBeUsedOnInheritingMemberAnalyzer
    • PrimaryConstructorParameterShouldBeReadOnlyAnalyzer
    • LocalVariablesShouldNotHideSymbolsAnalyzer
  • Add regression tests for MA0196, including a C# 12 primary-constructor case.
  • Update MA0196 documentation to explicitly state that primary constructors are ignored.

Notes

  • MA0196 uses includeRecordDeclarations: true in the shared helper to cover record primary constructors.
  • Targeted analyzer tests were run on default Roslyn plus roslyn4.2 and roslyn4.14.

Fix #1139

Skip MA0196 diagnostics on primary constructors, whose XML docs belong to the containing type. Extract a shared IsPrimaryConstructor symbol helper and reuse it where primary-constructor detection was duplicated.

Add regression tests and update MA0196 documentation to reflect the behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meziantou meziantou merged commit 6db417e into main May 8, 2026
13 checks passed
@meziantou meziantou deleted the meziantou/ignore-primary-ctors branch May 8, 2026 14:57
This was referenced May 8, 2026
This was referenced Jun 11, 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.

MA1096 (inheritdoc on non-override) triggers for inheritdoc on type

1 participant