Skip to main content

UseTrimEntries

Diagnostic Rule Overview

FieldValue
IDSHIMMER1031
Analyzer titleUse StringSplitOptions.TrimEntries
Analyzer messageUse the overload of string.Split with StringSplitOptions.TrimEntries to trim entries
Code fix titleUse StringSplitOptions.TrimEntries
Default severityInfo
Minimum framework/language version.NET 5.0
CategoryShimmeringUsage
Link to codeUseTrimEntriesAnalyzer.cs
Code fix exists?Yes

Detailed Explanation

In .NET 5.0 and later, string.Split supports StringSplitOptions.TrimEntries which trims individual substrings as they are parsed, avoiding intermediate allocations. Calling string.Split() followed by Select(x => x.Trim()) (and optionally ToArray()) is less efficient and more verbose.

Examples

Flagged code:

using System;
using System.Linq;

namespace Tests;
class Test
{
void Do(string input)
{
var x = input.Split(',').Select(x => x.Trim());
}
}

Fixed code:

using System;
using System.Linq;

namespace Tests;
class Test
{
void Do(string input)
{
var x = input.Split(',', StringSplitOptions.TrimEntries);
}
}

Justification of the Severity

Using StringSplitOptions.TrimEntries avoids creating intermediate strings and allocations from the split operation before they are trimmed, leading to cleaner code and better performance. Because it is a recommendation and requires .NET 5.0+, the severity is set to Info.