dotnet / dotnet/sdk

`CA1874` should catch cases where Regex.Count() is compared to Zero

Open
#53,261 0 comments 0 reactions 0 assignees View on GitHub
Area-Microsoft.CodeAnalysis.NetAnalyzers untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Is your feature request related to a problem? Please describe.
ignoring GeneratedRegex, this code should raise CA1874, to replace the `...Count(...) == 0` with `! .. .IsMatch(...)`.
Simmilarly:
- `...Count(...) > 0` => `.IsMatch(...)`
- `...Count(...) != 0` => `.IsMatch(...)`
```cs
internal class Class1
{
public void Do()
{
if (new Regex("abc").Count("abc") == 0)
throw new InvalidOperationException();
}
}
```
### Describe the solution you'd like
the Analyzer(s) should be updated to include these zero-compare cases. Perhaps even if `CA1875` is not enabled

### Additional context
in our cases these snippets are only used once in that statement. they are not using MatchCollection/Count in any other way

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.