roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

RCS0055 doesn't know how to deal with concatenated strings as arguments

Open fgimian opened this issue 4 years ago • 4 comments
trafficstars

Product and Version Used: 3.2.0

Steps to Reproduce:

  1. Create a new console project using .NET 5

  2. Install Roslynator.Analyzers

  3. Create an .editorconfig with the following contents

    [*.cs]
    dotnet_diagnostic.RCS0055.severity = error
    
  4. Update your Program.cs as follows

    using System;
    
    namespace RoslynatorProblem
    {
        internal static class Program
        {
            internal static void Main()
            {
                Console.WriteLine(
                    "some text "
                    + "some more text");
            }
        }
    }
    

Actual Behavior:

RCS0055 is raised for fix formatting of a binary expression chain on the line where the string is defined in the Console.WriteLine call.

Expected Behavior:

I believe this should not cause an error, or perhaps there can be some guidance in the docs for how one should deal with this particular situation. I'm uncertain if there's a way to solve this or if it's a bug.

fgimian avatar Aug 05 '21 11:08 fgimian

I would like to clarify what you are suggesting: Do you want not to indent string concatentation or any "add expression" or anything else?

josefpihrt avatar Aug 19 '21 17:08 josefpihrt

I'd like to sincerely apologise for missing your reply and question. The reason that one may choose to concatenate strings like this is when enforcing a line limit and the string is very long.

As you can see here, using + is more performant than using something like string.Concat.

Please let me know your thoughts 😄 Fotis

fgimian avatar Mar 12 '22 01:03 fgimian

I think this issue need more clarification.

Are you suggesting not to indent "add operator" on multiple lines if the "add operator" is actually string concatenation?

Current behavior

Code with diagnostic

    Console.WriteLine(
        "first"
        + "second"
        + "third");

Code with fix:

    Console.WriteLine(
        "first"
            + "second"
            + "third");

Proposed behavior

Code with no diagnostic:

    Console.WriteLine(
        "first"
        + "second"
        + "third");

Code with diagnostic (not string concatenation):

    Console.WriteLine(
        1
        + 2
        + 3);

josefpihrt avatar Mar 12 '22 17:03 josefpihrt

I think this issue need more clarification.

Are you suggesting not to indent "add operator" on multiple lines if the "add operator" is actually string concatenation?

Current behavior

Code with diagnostic

    Console.WriteLine(
        "first"
        + "second"
        + "third");

Code with fix:

    Console.WriteLine(
        "first"
            + "second"
            + "third");

Proposed behavior

Code with no diagnostic:

    Console.WriteLine(
        "first"
        + "second"
        + "third");

Code with diagnostic (not string concatenation):

    Console.WriteLine(
        1
        + 2
        + 3);

Thanks for the reply. Actually your post answers my original question. I think it may be worth explaining what the required fix is in the docs for such an example as I couldn't quite figure out what Rsolynator is after using the docs.

Now that I see your solution, I agree with what Roslynator is suggesting. Having the subsequent lines indented creates a visual distincition between those and other method arguments.

So my only suggestion out of this ticket would be to maybe update the docs with the examples you showed above too.

I'm happy to close the ticket if you're happy too.

Huge love and thanks! Fotis

fgimian avatar Mar 13 '22 05:03 fgimian

I think it's safe to close this one now 😄

fgimian avatar Sep 28 '22 00:09 fgimian