Skip to content

Report MA0051 on expression bodies of properties, indexers, and operators - #1517

Merged
meziantou merged 1 commit into
mainfrom
feature/ma0051-expression-bodies-9ad2b7
Sep 12, 2026
Merged

meziantou merged 1 commit into
mainfrom
feature/ma0051-expression-bodies-9ad2b7

Conversation

@meziantou

Copy link
Copy Markdown
Owner

What

MA0051 (Method is too long) never reported some member bodies:

  • PropertyDeclarationSyntax.ExpressionBody was never passed to the analysis. Only the accessor list was inspected, so int P => /* 80 lines */; was invisible to the rule while the very same body on a method was reported.
  • IndexerDeclaration, OperatorDeclaration, ConversionOperatorDeclaration and EventDeclaration were not registered in Initialize, so no body on those members was counted at all.

Changes

  • PropertyDeclarationSyntax: analyze node.ExpressionBody, reported on the property identifier.
  • Register and handle the missing member kinds:
    • indexers — expression body reported on this, plus the accessors;
    • operators — reported on the operator token (+, -, …);
    • conversion operators — reported on the implicit / explicit keyword;
    • events — the add / remove accessors.
  • Extract the accessor loop into an AnalyzeAccessors helper shared by properties, indexers, and events.
  • Document the members covered by the rule in docs/Rules/MA0051.md.

Notes for reviewers

  • This turns previously silent code into warnings, so a project with a long expression-bodied property or operator can start seeing MA0051 where it did not before. That is the intent: the rule already reported the identical body on a method.
  • The reported location for each new member kind is the closest analogue of the method identifier, so the squiggle lands on the member name rather than the whole body.

Testing

  • 9 new tests in MethodShouldNotBeTooLongAnalyzerTests covering property expression bodies (reporting and non-reporting), indexer expression bodies and accessors, event accessors, and operator / conversion-operator block and expression bodies.
  • dotnet build: succeeds, 0 warnings.
  • MethodShouldNotBeTooLongAnalyzerTests: 24/24 pass on Roslyn 4.8, 4.14, 5.0, 5.6 and 5.9 (15 tests before the change).
  • dotnet run --project src/DocumentationGenerator: exits 0, no generated markdown changed.

…tors

MA0051 only looked at the accessor list of a property, so an expression-bodied
property such as `int P => /* 80 lines */;` was never counted, while the same
body on a method was reported. Indexers, operators, conversion operators, and
events were not registered at all, so no body on those members was counted.

Pass `PropertyDeclarationSyntax.ExpressionBody` to the analysis, register the
missing member kinds, and share the accessor loop between properties, indexers,
and events.
@meziantou
meziantou merged commit cf8b5c1 into main Sep 12, 2026
13 checks passed
@meziantou
meziantou deleted the feature/ma0051-expression-bodies-9ad2b7 branch September 12, 2026 20:18
This was referenced Sep 12, 2026
This was referenced Sep 26, 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