dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.55k stars 4.54k forks source link

[Analyzer]: Extend string IndexOf => Contains analyzers to Span IndexOf/IndexOfAny/IndexOfAnyExcept #87691

Open MihaZupan opened 1 year ago

MihaZupan commented 1 year ago

We have an existing analyzer/fixer CA2249 that flags patterns like string.IndexOf('a') >= 0 and suggests replacing the call with string.Contains('a').

We should consider expanding that to also flag patterns like

and their inverse variants

Unlike with strings, these shouldn't run into issues w.r.t. culture.

Suggested category: usage / performance Suggested severity: suggestion

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-memory See info in area-owners.md if you want to be subscribed.

Issue Details
We have an existing analyzer/fixer [CA2249](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2249) that flags patterns like `string.IndexOf('a') >= 0` and suggests replacing the call with `string.Contains('a')`. We should consider expanding that to also flag patterns like - `span.IndexOf('a') >= 0` => `span.Contains('a')` - `span.IndexOfAny('a', 'b') >= 0` => `span.ContainsAny('a', 'b')` - `span.IndexOfAnyExcept('a', 'b') >= 0` => `span.ContainsAnyExcept('a', 'b')` Unlike with strings, these shouldn't run into issues w.r.t. culture. Suggested category: usage / performance Suggested severity: suggestion
Author: MihaZupan
Assignees: -
Labels: `area-System.Memory`, `code-analyzer`, `code-fixer`
Milestone: -