Skip to content

Consider using interceptors for IndexOfAny-like calls #91743

@MihaZupan

Description

@MihaZupan

dotnet/roslyn-analyzers#6898 is adding an analyzer that will flag uses like

const string MyValues = "abcdef";

span.IndexOfAny(MyValues);

and offer a fixer to replace it with

private static readonly SearchValues<char> s_myValues = SearchValues.Create(MyValues);
const string MyValues = "abcdef";

span.IndexOfAny(s_myValues);

For patterns like these, we should consider using interceptors to automagically rewrite them to use SearchValues, without requiring the user to extract the values to a field.
The logic could also be smarter and rewrite the above example to use IndexOfAnyInRange directly instead.

This would also help projects that multi-target to TFMs that don't have SearchValues, as it would let them take advantage of the perf improvements without extra #if #elses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Memoryneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions