Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 11, 2025

Implementation completed for JsonDocument.Parse().RootElement analyzer:

  • Create analyzer to detect JsonDocument.Parse().RootElement pattern without disposal (CA2026)
  • Create code fixer to replace with JsonElement.Parse()
  • Add resource strings for diagnostic messages
  • Create comprehensive unit tests for analyzer
  • Create comprehensive unit tests for code fixer
  • Build and test implementation
  • Run dotnet format
  • Request code review (passed with no issues)
  • Regenerate auto-generated documentation files
  • Address code review feedback (round 1)
  • Address code review feedback (round 2)
  • Address code review feedback (round 3)

Summary

Successfully implemented CA2026 analyzer and code fixer for detecting JsonDocument.Parse().RootElement patterns without disposal. The analyzer helps developers avoid resource leaks by suggesting the use of JsonElement.Parse() instead.

Key features:

  • Detects immediate RootElement access after JsonDocument.Parse()
  • Only suggests fix when JsonElement.Parse has matching overload with same parameter types
  • Automatic code fix replaces entire pattern with JsonElement.Parse()
  • 13 comprehensive unit tests all passing (8 original + 5 new corner cases)
  • Uses WellKnownTypeNames and optimized loops for performance
  • Consolidated validation logic for cleaner code

Test coverage includes:

  • Basic positive and negative cases
  • Proper disposal detection (using statements/declarations)
  • Mismatched overloads (no diagnostic when JsonElement.Parse doesn't have matching signature)
  • Chained property access
  • Variable storage pattern
  • Lambda expressions
  • Multiple parameter overloads

Files changed:

  • Analyzer: PreferJsonElementParse.cs
  • Fixer: PreferJsonElementParse.Fixer.cs
  • Tests: PreferJsonElementParseTests.cs
  • Resources: MicrosoftNetCoreAnalyzersResources.resx
  • Config: DiagnosticCategoryAndIdRanges.txt, AnalyzerReleases.Unshipped.md, WellKnownTypeNames.cs
  • Auto-generated docs: Microsoft.CodeAnalysis.NetAnalyzers.md, Microsoft.CodeAnalysis.NetAnalyzers.sarif
Original prompt

Implement a new Roslyn analyzer and associated code fixer in the dotnet/sdk repo (netanalyzers) to address dotnet/runtime#111957. The analyzer should flag common patterns where developers obtain a JsonElement by accessing JsonDocument.Parse("json").RootElement without disposing the JsonDocument, e.g.:

JsonElement element = JsonDocument.Parse("json").RootElement;

The analyzer should recommend switching to the new JsonElement.Parse methods approved in dotnet/runtime#108930 for .NET 10. The code fixer should automatically replace the flagged patterns with calls to JsonElement.Parse, improving code correctness and resource management.

Include:

  • Analyzer code to detect patterns described in the issue.
  • Code fixer for automatic replacement.
  • Unit tests for both analyzer and fixer.
  • Documentation updates if necessary.

This pull request was created as a result of the following prompt from Copilot chat.

Implement a new Roslyn analyzer and associated code fixer in the dotnet/sdk repo (netanalyzers) to address dotnet/runtime#111957. The analyzer should flag common patterns where developers obtain a JsonElement by accessing JsonDocument.Parse("json").RootElement without disposing the JsonDocument, e.g.:

JsonElement element = JsonDocument.Parse("json").RootElement;

The analyzer should recommend switching to the new JsonElement.Parse methods approved in dotnet/runtime#108930 for .NET 10. The code fixer should automatically replace the flagged patterns with calls to JsonElement.Parse, improving code correctness and resource management.

Include:

  • Analyzer code to detect patterns described in the issue.
  • Code fixer for automatic replacement.
  • Unit tests for both analyzer and fixer.
  • Documentation updates if necessary.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Implement Roslyn analyzer for JsonElement disposal patterns Add CA2026 analyzer: Prefer JsonElement.Parse over JsonDocument.Parse().RootElement Oct 11, 2025
@Copilot Copilot AI requested a review from stephentoub October 11, 2025 17:19
Copilot finished work on behalf of stephentoub October 11, 2025 17:19
@stephentoub
Copy link
Member

@copilot, there are some build failures:

  One or more auto-generated documentation files were either edited manually, or not updated. Please revert changes made to the following files (if manually edited) and run `msbuild /t:pack` at the root of the repo to automatically update them:
      D:\a\_work\1\s\src\Microsoft.CodeAnalysis.NetAnalyzers\src\Microsoft.CodeAnalysis.NetAnalyzers.md
      D:\a\_work\1\s\src\Microsoft.CodeAnalysis.NetAnalyzers\src\Microsoft.CodeAnalysis.NetAnalyzers.sarif

Copy link
Contributor Author

Copilot AI commented Oct 11, 2025

@copilot, there are some build failures:

  One or more auto-generated documentation files were either edited manually, or not updated. Please revert changes made to the following files (if manua...

Fixed in commit b4387d1. Ran msbuild /t:pack at the repository root to regenerate the auto-generated documentation files (Microsoft.CodeAnalysis.NetAnalyzers.md and Microsoft.CodeAnalysis.NetAnalyzers.sarif) which now include the CA2026 rule information.

Copilot finished work on behalf of stephentoub October 11, 2025 19:45
@stephentoub stephentoub marked this pull request as ready for review October 11, 2025 22:42
@stephentoub stephentoub requested a review from a team as a code owner October 11, 2025 22:42
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().RootElement patterns 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]>
@Copilot Copilot AI requested a review from stephentoub October 12, 2025 03:00
Copilot finished work on behalf of stephentoub October 12, 2025 03:00
- 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]>
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.

2 participants