Skip to main content

MissingRemoveEmptyEntries

Diagnostic Rule Overview​

FieldValue
IDSHIMMER1030
Analyzer titleUse StringSplitOptions.RemoveEmptyEntries
Analyzer messageUse the overload of String.Split with StringSplitOptions.RemoveEmptyEntries to remove empty entries
Code fix titleOptimize string.Split()
Default severityInfo
Minimum framework/language versionN/A
CategoryShimmeringUsage
Link to codeMissingRemoveEmptyEntriesAnalyzer.cs
Code fix exists?Yes

Detailed Explanation​

The StringSplitOptions.RemoveEmptyEntries parameter allows you to remove empty entries in a single String.Split() call and therefore is more efficient than chaining with a LINQ call or two to do the same thing.

Currently, the checked patterns in the .Where() call are:

x => x.Length > 0
x => x.Length != 0
x => x.Length >= 1
x => x != ""
x => x != string.Empty
x => !string.IsNullOrEmpty(x)
x => x.Any()
x => x is not ""

Note that this diagnostic is not triggered if StringSplitOptions is already used.

Examples​

Flagged code:

using System;
using System.Linq;

namespace Tests;
class Test
{
void Do(string input)
{
var x = input.Split(' ')
.Where(x => x.Length > 0);
}
}

Fixed code:

using System;
using System.Linq;

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

Justification of the Severity​

The code fix improves performance while keeping the code readable, but the flagged code does not cause any bugs or errors.