Skip to content

[New rule suggestion]: Use Split(char, ....) or Split(string, ....) overloads when available #41826

Description

@Youssef1313

Describe the problem you are trying to solve

Avoid unnecessary array allocations if an array is created only to be passed to string.Split method.

Describe suggestions on how to achieve the rule

  1. If one of the string.Split overloads that takes an array is used, and the array contains only one element, and not used anywhere else, the analyzer should suggest using either Split(char, ...) overload or Split(string, ...) overload.

Additional context

Example from dotnet/sdk:

https://github.com/dotnet/sdk/blob/ab2176b3447e3e7820b5a340408110b6d498f907/src/Tasks/Microsoft.NET.Build.Tasks/CompilationOptionsConverter.cs#L20

Using the following will avoid an unnecessary array allocation:

                compilerOptionsItem.GetMetadata("DefineConstants")?.Split(';', StringSplitOptions.RemoveEmptyEntries),

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Runtimecode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixer

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions