Skip to content

Conversation

Alex-Sob
Copy link

@Alex-Sob Alex-Sob commented Sep 11, 2025

I would like to suggest a couple of changes that can improve readability of code analyzer tests:

  • Use raw string literals for C# test source code in all unit tests for code analyzers.

  • I would suggest to apply [StringSyntax("C#-test")] to all helper methods used to run analyzer tests, for example:

    public static class CSharpAnalyzerVerifier<TAnalyzer>
    {
      public static async Task VerifyAnalyzerAsync([StringSyntax("C#-test")] string source, params DiagnosticResult[] expected) { ... }
    }

    This should enable syntax highlighting for test C# source code in unit tests for analyzers. Currently the code in a test might look like this:

image

With raw string literals and StringSyntax attribute, this is what it will look like (I included such a test in #63641):

image

Also VS draws a nice vertical line for raw string literals, which also means that there's no extra whitespace before the code so that it does not have to start from the very left overflowing the test.

@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Sep 11, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Sep 11, 2025
@martincostello martincostello added analyzer Indicates an issue which is related to analyzer experience and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Sep 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer Indicates an issue which is related to analyzer experience community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants