StringBuilder instances that never build a string clutter the code and worse are a drag on performance. Either they
should be removed, or the missing ToString() call should be added.
public void DoSomething(List<string> strings) {
var sb = new StringBuilder(); // Noncompliant
sb.Append("Got: ");
foreach(var str in strings) {
sb.Append(str).Append(", ");
// ...
}
}
public void DoSomething(List<string> strings) {
foreach(var str in strings) {
// ...
}
}
or
public void DoSomething(List<string> strings) {
var sb = new StringBuilder();
sb.Append("Got: ");
foreach(var str in strings) {
sb.Append(str).Append(", ");
// ...
}
logger.LogInformation(sb.ToString());
}
No issue is reported when StringBuilder is:
sb.CopyTo(), sb.GetChunks(), sb.Length, or sb[index]. ToString() invocation there. var sb = GetStringBuilder();).