dotnet / roslyn-analyzers

MIT License
1.56k stars 462 forks source link

Add CollectionExpression support to CA1870 (UseSearchValues) #7252

Closed MihaZupan closed 3 months ago

MihaZupan commented 4 months ago

Followup after #6898 to add recognition for collection expression patterns as well.

Currently, if you write something like

private static readonly char[] s_chars = new[] { 'a', 'e', 'i', 'o', 'u', 'A' };
public static int Test(ReadOnlySpan<char> s) => s.IndexOfAny(s_chars);

you will be offered two fixers: one to replace s_chars with SearchValues.Create("aeiouA"), and the other to replace the array creation with ['a', 'e', 'i', 'o', 'u', 'A']. If you accept the latter, you won't be offered the SearchValues fixer anymore since we don't recognize the pattern.

This PR adds recognition for patterns like

static readonly char[] s_chars = ['a', 'e', 'i', 'o', 'u', 'A'];

text.IndexOfAny(['a', 'e', 'i', 'o', 'u', 'A']);
text.IndexOfAny([(byte)'a', (byte)'e', (byte)'i', (byte)'o', (byte)'u', (byte)'A']);
text.IndexOfAny(s_chars);
MihaZupan commented 4 months ago

cc: @stephentoub

codecov[bot] commented 4 months ago

Codecov Report

Attention: Patch coverage is 86.82171% with 17 lines in your changes are missing coverage. Please review.

Project coverage is 96.47%. Comparing base (de3a920) to head (3692f88). Report is 1 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #7252 +/- ## ========================================= Coverage 96.47% 96.47% ========================================= Files 1436 1440 +4 Lines 342881 344487 +1606 Branches 11292 11340 +48 ========================================= + Hits 330797 332356 +1559 - Misses 9230 9262 +32 - Partials 2854 2869 +15 ```
MihaZupan commented 3 months ago

@dotnet/roslyn-analysis who would be the right person to review this one?