Report MA0051 on expression bodies of properties, indexers, and operators

Merged
ME
2mo ago
+198-8

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.