StringBuilder.Append is given the result of ToString(), which allocates a string the builder did not need
What it finds
Append has overloads for numbers, characters and other values that write them straight into the builder; Append(x.ToString()) creates a temporary string first. Calls of ToString with a format argument are left alone.
From the rule's source, append_of_to_string.rs.
using System.Text; class Report{ string Build(int count) { var sb = new StringBuilder(); sb.Append(count.ToString());tx0 reports: ToString() makes a string that Append does not need sb.Append(count); return sb.ToString(); }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:append-of-to-string" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:append-of-to-string] reason="..."tx0 explain tx0/csharp:append-of-to-string shows the same in the terminal. Found a false positive? Report it.