Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 27, 2025

The SDK repository lists four code analysis rules without corresponding documentation.

Changes

Added complete documentation for:

  • CA1873: Avoid potentially expensive logging - Flags expensive operations (object creation, invocations, string joins) in logging arguments that execute regardless of log level. Recommends ILogger.IsEnabled guards.

  • CA1874: Use 'Regex.IsMatch' - Suggests replacing Regex.Match(...).Success with the more efficient Regex.IsMatch(...).

  • CA1875: Use 'Regex.Count' - Suggests replacing Regex.Matches(...).Count with the more efficient Regex.Count(...).

  • CA2023: Invalid braces in message template - Detects malformed braces in logging templates that cause runtime errors (unmatched, improperly nested).

Each includes rule metadata, violation/fix examples in C# and VB, suppression guidance, and API references. Updated index.md with entries in correct sequence.

Example

// CA1873 violation
_logger.LogDebug($"Processing {string.Join(", ", data)} items");

// Fixed
if (_logger.IsEnabled(LogLevel.Debug))
{
    _logger.LogDebug($"Processing {string.Join(", ", data)} items");
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/dotnet/roslyn-analyzers/contents/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md
    • Triggering command: curl -s REDACTED (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Document new code analysis rules</issue_title>
<issue_description>### Describe the issue or suggestion

https://github.com/dotnet/sdk/blob/main/src/Microsoft.CodeAnalysis.NetAnalyzers/src/RulesMissingDocumentation.md</issue_description>

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


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/fundamentals/code-analysis/quality-rules/ca1873.md CA1873: Avoid potentially expensive logging
docs/fundamentals/code-analysis/quality-rules/ca1874.md "CA1874: Use 'Regex.IsMatch' (code analysis)"
docs/fundamentals/code-analysis/quality-rules/ca1875.md CA1875: Use 'Regex.Count'
docs/fundamentals/code-analysis/quality-rules/ca2023.md CA2023: Invalid braces in message template
docs/fundamentals/code-analysis/quality-rules/index.md docs/fundamentals/code-analysis/quality-rules/index
docs/fundamentals/code-analysis/quality-rules/performance-warnings.md Performance rules
docs/fundamentals/code-analysis/quality-rules/reliability-warnings.md Reliability rules (code analysis)
docs/navigate/tools-diagnostics/toc.yml docs/navigate/tools-diagnostics/toc

Added complete documentation for:
- CA1873: Avoid potentially expensive logging
- CA1874: Use 'Regex.IsMatch'
- CA1875: Use 'Regex.Count'
- CA2023: Invalid braces in message template

Each documentation includes:
- Rule description and cause
- How to fix violations with code examples in both C# and VB
- When to suppress warnings
- Related links and API references
- Updated index.md with all four rules in correct sequence

Co-authored-by: gewarren <[email protected]>
Added missing metadata fields to match existing rule documentation:
- ms.topic: reference
- author: gewarren

Co-authored-by: gewarren <[email protected]>
Copilot AI changed the title [WIP] Document new code analysis rules Document code analysis rules CA1873, CA1874, CA1875, CA2023 Oct 27, 2025
Copilot AI requested a review from gewarren October 27, 2025 18:38
Copilot finished work on behalf of gewarren October 27, 2025 18:38
@gewarren gewarren marked this pull request as ready for review October 27, 2025 22:29
@gewarren gewarren requested a review from a team as a code owner October 27, 2025 22:29
Copilot AI review requested due to automatic review settings October 27, 2025 22:29
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 PR documents four previously undocumented code analysis rules from the .NET SDK repository. The changes add comprehensive documentation for CA1873 (expensive logging operations), CA1874 (Regex.IsMatch optimization), CA1875 (Regex.Count optimization), and CA2023 (invalid message template braces). Each new documentation page includes rule metadata, violation examples in both C# and Visual Basic, fix examples, suppression guidance, and relevant API references.

Key Changes

  • Added four new rule documentation files with complete examples and guidance
  • Updated index and category pages to include the new rules
  • Updated table of contents to link to the new rule pages

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
ca1873.md Documents CA1873 rule for avoiding expensive logging operations with IsEnabled guards
ca1874.md Documents CA1874 rule for preferring Regex.IsMatch over Match().Success
ca1875.md Documents CA1875 rule for preferring Regex.Count over Matches().Count
ca2023.md Documents CA2023 rule for detecting invalid braces in logging message templates
index.md Adds entries for the four new rules in the main rules index table
performance-warnings.md Adds CA1873, CA1874, and CA1875 to the performance rules listing
reliability-warnings.md Adds CA2023 to the reliability rules listing
toc.yml Adds table of contents entries for all four new rule pages

@gewarren gewarren mentioned this pull request Oct 28, 2025
Copy link
Contributor

@meaghanlewis meaghanlewis left a comment

Choose a reason for hiding this comment

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

These changes LGTM

@gewarren gewarren merged commit eca11f7 into main Oct 28, 2025
10 checks passed
@gewarren gewarren deleted the copilot/document-new-code-analysis-rules branch October 28, 2025 23:37
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.

Document new code analysis rules

3 participants