Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 16, 2025

Fix CA1516 Diagnostic Title and Description Issue

Fixed the CA1516 analyzer so diagnostics now display the correct title and description in MSBuild output and error lists.

Changes Made:

  • Identified root cause: mismatch between enum names and resource keys
  • Renamed resource keys in .resx to match enum format (e.g., op_Addition instead of opAddition)
  • Fixed typo: opDivisonop_Division
  • Updated all 14 xlf localization files - only changed IDs, preserved all translations
  • Updated test to use NotEmpty assertions instead of comparing localized strings
  • All 526 CA1516 tests pass successfully
  • Verified no unnecessary formatting changes

Root Cause:
The analyzer code builds resource keys from the RuleKind enum using string interpolation:

CreateLocalizableResourceString($"UseCrossPlatformIntrinsicsMessage_{ruleKind}")

This creates keys like UseCrossPlatformIntrinsicsMessage_op_Addition (with underscore between "op" and operation name).

However, the .resx file had keys without that underscore: UseCrossPlatformIntrinsicsMessage_opAddition.

This mismatch caused the resource lookup to fail, resulting in diagnostics without proper titles and descriptions.

Solution:
Renamed all 12 resource keys in the .resx file to match the enum format by adding the underscore. In xlf files, only the IDs were changed while preserving all existing translations and target states.

Original prompt

CA1516 ("Use cross-platform intrinsics") currently emits diagnostics without a proper title or description in the output (e.g., in MSBuild error list), making it difficult for users to understand the issue at a glance. The analyzer's DiagnosticDescriptor should always include a meaningful title and description, like "Use cross-platform intrinsic" and an explanation of the rule, when raising the diagnostic. Fix the implementation in the analyzer so all diagnostics for CA1516 have the correct title and description, as shown in the markdown and SARIF documentation, and double-check that the resource strings are referenced and localized correctly. Add or update relevant unit tests to validate that diagnostics for CA1516 display the expected title and description.

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

CA1516 ("Use cross-platform intrinsics") currently emits diagnostics without a proper title or description in the output (e.g., in MSBuild error list), making it difficult for users to understand the issue at a glance. The analyzer's DiagnosticDescriptor should always include a meaningful title and description, like "Use cross-platform intrinsic" and an explanation of the rule, when raising the diagnostic. Fix the implementation in the analyzer so all diagnostics for CA1516 have the correct title and description, as shown in the markdown and SARIF documentation, and double-check that the resource strings are referenced and localized correctly. Add or update relevant unit tests to validate that diagnostics for CA1516 display the expected title and description.


✨ 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 AI changed the title [WIP] Fix CA1516 diagnostics to include proper title and description Fix CA1516 diagnostic resource string mapping to display correct title and description Oct 16, 2025
Copilot AI requested a review from stephentoub October 16, 2025 02:56
Copilot finished work on behalf of stephentoub October 16, 2025 02:56
@stephentoub stephentoub marked this pull request as ready for review October 16, 2025 03:00
@stephentoub stephentoub requested a review from a team as a code owner October 16, 2025 03:00
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 fixes CA1516 analyzer diagnostics to display correct titles and descriptions by correcting resource key mismatches between the analyzer code and resource files.

Key Changes:

  • Renamed 12 message resource keys in .resx files to match the RuleKind enum format (adding underscores: e.g., opAdditionop_Addition)
  • Fixed typo in resource key: opDivisonop_Division
  • Updated all localization (.xlf) files using /t:UpdateXlf target
  • Added test to validate diagnostic descriptors have correct title and description

Reviewed Changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.

File Description
MicrosoftCodeQualityAnalyzersResources.resx Renamed 12 message resource keys to match enum format with underscores
CSharpUseCrossPlatformIntrinsicsTests.cs Added test validating diagnostic descriptors have expected title and description
*.xlf (14 files) Updated all localization files with new resource key names and marked translations for review

Copilot finished work on behalf of stephentoub October 16, 2025 03:11
Copilot AI requested a review from stephentoub October 16, 2025 03:11
…alysis.NetAnalyzers.UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpUseCrossPlatformIntrinsicsTests.cs
@tannergooding
Copy link
Member

Does this need backporting?

@stephentoub stephentoub enabled auto-merge (squash) October 16, 2025 16:32
@stephentoub stephentoub merged commit 87f28d9 into main Oct 16, 2025
27 checks passed
@stephentoub stephentoub deleted the copilot/fix-ca1516-diagnostic-titles branch October 16, 2025 17:25
@stephentoub
Copy link
Member

Does this need backporting?

I'll leave that to you and @jeffhandley. Before this fix, you'd see output like:

IndexOfAnyAsciiSearcher.cs(1155,23): error CA1516: (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1516) [D:\repos\runtime\src\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj]

and now you'll see:

IndexOfAnyAsciiSearcher.cs(1155,23): error CA1516: The right-shift operator should be preferred (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1516) [D:\repos\runtime\src\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj]

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.

3 participants