Report MA0051 on expression bodies of properties, indexers, and operators
Merged
ME
2mo ago+198+198+012345678901234567890123456789-8-8-0123456789
What
MA0051 (Method is too long) never reported some member bodies:
PropertyDeclarationSyntax.ExpressionBodywas never passed to the analysis. Only the accessor list was inspected, soint P => /* 80 lines */;was invisible to the rule while the very same body on a method was reported.IndexerDeclaration,OperatorDeclaration,ConversionOperatorDeclarationandEventDeclarationwere not registered inInitialize, so no body on those members was counted at all.
Changes
PropertyDeclarationSyntax: analyzenode.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/explicitkeyword; - events — the
add/removeaccessors.
- indexers — expression body reported on
- Extract the accessor loop into an
AnalyzeAccessorshelper 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
MethodShouldNotBeTooLongAnalyzerTestscovering 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.