Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 5, 2025

Adds missing documentation for CA2026, a .NET 10 performance analyzer that flags inefficient JSON parsing patterns.

Changes

  • New rule documentation (ca2026.md): Explains why JsonElement.Parse() is preferred over JsonDocument.Parse().RootElement in .NET 10+
    • JsonDocument implements IDisposable and rents from ArrayPool<byte>, requiring disposal to avoid memory leaks
    • JsonElement.Parse() parses directly without allocation overhead or disposal requirements
  • Index updates: Added CA2026 entries to performance-warnings.md and index.md
  • External code snippets: Created compilable C# and VB code examples in snippets/csharp/all-rules/ca2026.cs and snippets/vb/all-rules/ca2026.vb
    • Uses conditional compilation to handle .NET 10 API availability
    • Both projects compile successfully with existing all-rules projects

Example

// Violation: Creates disposable JsonDocument, rents from ArrayPool
JsonElement element = JsonDocument.Parse(json).RootElement;

// Fixed: Direct parse, no disposal needed
JsonElement element = JsonElement.Parse(json);

Closes #49504

Original prompt

This section details on the original issue you should resolve

<issue_title>Add doc for CA2026</issue_title>
<issue_description>CA2026: Prefer JsonElement.Parse over JsonDocument.Parse().RootElement is also missing documentation.

Originally posted by @xtqqczze in #49492 (comment)


Associated WorkItem - 505743</issue_description>

Comments on the Issue (you are @copilot in this section)


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


Internal previews

📄 File 🔗 Preview link
docs/fundamentals/code-analysis/quality-rules/ca2026.md CA2026: Prefer JsonElement.Parse over JsonDocument.Parse().RootElement
docs/fundamentals/code-analysis/quality-rules/index.md Code quality rules overview
docs/fundamentals/code-analysis/quality-rules/reliability-warnings.md Reliability rules
docs/navigate/tools-diagnostics/toc.yml docs/navigate/tools-diagnostics/toc

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Copilot AI changed the title [WIP] Add documentation for CA2026 JsonElement.Parse Add documentation for CA2026: Prefer JsonElement.Parse over JsonDocument.Parse().RootElement Nov 5, 2025
Copilot AI requested a review from gewarren November 5, 2025 21:48
Copilot finished work on behalf of gewarren November 5, 2025 21:48
@gewarren gewarren marked this pull request as ready for review November 6, 2025 01:26
@gewarren gewarren requested a review from a team as a code owner November 6, 2025 01:26
Copilot AI review requested due to automatic review settings November 6, 2025 01:26
Copy link
Contributor

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 pull request adds documentation for a new code analysis rule CA2026, which recommends using JsonElement.Parse() instead of JsonDocument.Parse().RootElement for better performance and resource management in .NET 10.

Key changes:

  • Adds complete documentation for CA2026 rule with C# and VB examples.
  • Updates the table of contents, rule index, and reliability warnings list to include CA2026.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
docs/fundamentals/code-analysis/quality-rules/ca2026.md New documentation file for CA2026 rule with examples and guidance
docs/navigate/tools-diagnostics/toc.yml Adds CA2026 to the navigation table of contents
docs/fundamentals/code-analysis/quality-rules/reliability-warnings.md Adds CA2026 to the reliability warnings table
docs/fundamentals/code-analysis/quality-rules/index.md Adds CA2026 to the main quality rules index
Comments suppressed due to low confidence (1)

docs/fundamentals/code-analysis/quality-rules/ca2026.md:1

  • The ms.topic: reference metadata field is missing from the frontmatter. This field is present in similar rule documentation files (like ca2024.md and ca2025.md) and should be included for consistency.
---

Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

Overall, this looks great. One comment.

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Copilot AI requested a review from gewarren November 6, 2025 16:53
Copilot finished work on behalf of gewarren November 6, 2025 16:53
@gewarren gewarren enabled auto-merge (squash) November 6, 2025 18:12
@gewarren gewarren merged commit ca93180 into main Nov 6, 2025
11 checks passed
@gewarren gewarren deleted the copilot/add-doc-for-ca2026 branch November 6, 2025 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add doc for CA2026

3 participants