Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use StringBuilder.Append(char) for single character strings #33786

Closed
terrajobst opened this issue Mar 19, 2020 · 4 comments · Fixed by dotnet/roslyn-analyzers#3481
Closed

Use StringBuilder.Append(char) for single character strings #33786

terrajobst opened this issue Mar 19, 2020 · 4 comments · Fixed by dotnet/roslyn-analyzers#3481
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer
Milestone

Comments

@terrajobst
Copy link
Contributor

It's common to see calls to StringBuilder.Append(string) with a const string containing a single character, e.g. ",". These would be slightly cheaper as calls using a const char instead.

Category: Performance

@terrajobst terrajobst added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer labels Mar 19, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Mar 19, 2020
@jeffhandley jeffhandley added this to the 5.0 milestone Mar 20, 2020
@jeffhandley jeffhandley added the code-fixer Marks an issue that suggests a Roslyn code fixer label Mar 21, 2020
@jeffhandley
Copy link
Member

Estimates:

  • Analyzer: Small
  • Fixer: Small

@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed untriaged New issue has not been triaged by the area owner api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Mar 21, 2020
@pgovind
Copy link

pgovind commented Apr 1, 2020

Quick question about a corner case here:

Consider the following code:

            const string aa = "a", bb = "b";
            sb.Append(aa); //sb is a StringBuilder

The analyzer would detect const string aa = "a" as the element to warn about, but Roslyn considers the whole line const string aa = "a", bb = "b"; as a single declaration node. We have 2 options here:

  1. If we want to offer a code fix, we should end up with the following code:
            const char aa = "a";
            const string bb = "b";
            sb.Append(aa); //sb is a StringBuilder

OR
2) We bail out when we detect multiple declarations and don't offer a code fix (but the analyzer could still report a "Prefer char over string" message)

Which do we want?

P.S: There is also the more straightforward case when we have:

            const string aa = "a", bb = "b";
            sb.Append(aa).Append(bb); //sb is a StringBuilder

where the code-fix is more obvious and just results in:

            const char aa = 'a', bb = 'b';

@jeffhandley
Copy link
Member

I recommend that we just bail on multiple declarations instead of trying to get clever.

@pgovind
Copy link

pgovind commented Apr 10, 2020

There are only 2 real corner cases to consider in this Analyzer:

  1. Multiple declarations in a single line. This has already been addressed above.
  2. Class fields declared as const strings. I decided to not tag this in the analyzer and only tag(and fix) local const string variable declarations.

1 issue to consider in the code-fix is that it can lead to build breaks in some cases. For ex:

// Assume we have the following method declaration
public string AMethod(string foo)
{

}

// Somewhere later we have
const string str = "a";
stringBuilder.Append(str); // This will tag the analyzer and suggest a code-fix
AMethod(str); // If the code-fix was invoked, this will now be a compiler error since str will now be a char.

@jeffhandley

@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants