Add CA2026 analyzer: Prefer JsonElement.Parse over JsonDocument.Parse().RootElement#51209
Conversation
…ttern Co-authored-by: stephentoub <[email protected]>
|
@copilot, there are some build failures: |
Co-authored-by: stephentoub <[email protected]>
Fixed in commit b4387d1. Ran |
There was a problem hiding this comment.
Pull Request Overview
This PR implements a new Roslyn analyzer (CA2026) that detects patterns where developers access JsonDocument.Parse().RootElement without proper disposal and suggests using JsonElement.Parse() instead to avoid resource leaks. The analyzer includes both diagnostic detection and an automatic code fixer.
- Implements CA2026 analyzer to detect
JsonDocument.Parse().RootElementpatterns without disposal - Provides automatic code fix to replace with
JsonElement.Parse()calls - Includes comprehensive unit tests covering various scenarios and edge cases
Reviewed Changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| PreferJsonElementParseTests.cs | Comprehensive unit tests for both analyzer and code fixer functionality |
| DiagnosticCategoryAndIdRanges.txt | Updates reliability rule range to include CA2026 |
| Multiple .xlf files | Localization updates with new resource strings marked as "new" |
| PreferJsonElementParse.cs | Main analyzer implementation detecting the problematic pattern |
| PreferJsonElementParse.Fixer.cs | Code fixer that replaces JsonDocument.Parse().RootElement with JsonElement.Parse() |
| MicrosoftNetCoreAnalyzersResources.resx | Resource strings for diagnostic messages |
| AnalyzerReleases.Unshipped.md | Adds CA2026 to unshipped analyzer releases |
| Microsoft.CodeAnalysis.NetAnalyzers.sarif | Auto-generated SARIF metadata for the new rule |
| Microsoft.CodeAnalysis.NetAnalyzers.md | Auto-generated documentation for the new analyzer |
- Use WellKnownTypeNames for type strings - Replace open-coded loops with LINQ - Remove unnecessary disposal checks (if RootElement is accessed directly off Parse, JsonDocument is never disposed) - Delete unused ContainsOperation and IsImmediateUseWithoutDisposal methods - Only raise diagnostic when JsonElement.Parse has matching overload with same parameter types Co-authored-by: stephentoub <[email protected]>
- Move jsonElementParseOverloads collection up and use it to check if JsonElement.Parse exists - Replace LINQ-based parameter matching with loop to avoid lambda captures and enable early exit - Improves performance by eliminating unnecessary allocations Co-authored-by: stephentoub <[email protected]>
- Consolidate multiple if checks into single conditions in both analyzer and fixer - Add test for case where JsonElement.Parse has no matching overload - Add test for chained property access after RootElement - Add test to verify no diagnostic when RootElement accessed via variable - Add test for multiple parameters with matching overload - Add test for pattern in lambda expressions - All 13 tests now passing Co-authored-by: stephentoub <[email protected]>
- Replace all @"..." verbatim string literals with """ ... """ raw string literals - Properly indent all test code for better readability - Makes tests easier to read and maintain - All 13 tests continue to pass Co-authored-by: stephentoub <[email protected]>
Implementation completed for JsonDocument.Parse().RootElement analyzer:
Summary
Successfully implemented CA2026 analyzer and code fixer for detecting
JsonDocument.Parse().RootElementpatterns without disposal. The analyzer helps developers avoid resource leaks by suggesting the use ofJsonElement.Parse()instead.Key features:
Test coverage includes:
Files changed:
PreferJsonElementParse.csPreferJsonElementParse.Fixer.csPreferJsonElementParseTests.csMicrosoftNetCoreAnalyzersResources.resxDiagnosticCategoryAndIdRanges.txt,AnalyzerReleases.Unshipped.md,WellKnownTypeNames.csMicrosoft.CodeAnalysis.NetAnalyzers.md,Microsoft.CodeAnalysis.NetAnalyzers.sarifOriginal prompt
This pull request was created as a result of the following prompt from Copilot chat.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.