Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Analyzer]: Regex.Matches(...).Count to Regex.Count(...) #111240

Open
stephentoub opened this issue Jan 9, 2025 · 2 comments · May be fixed by dotnet/roslyn-analyzers#7547
Open

[Analyzer]: Regex.Matches(...).Count to Regex.Count(...) #111240

stephentoub opened this issue Jan 9, 2025 · 2 comments · May be fixed by dotnet/roslyn-analyzers#7547
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Text.RegularExpressions code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@stephentoub
Copy link
Member

We should have an analyzer that looks for:

Regex.Matches(...).Count // or Count()

and replaces it with:

Regex.Count(...);

when the target environment has a Count overload with parameters that exactly match those of the used Matches overload. This applies to both the static and instance methods. Computing the Count directly is much cheaper than using Matches as it doesn't need to allocate all the Match objects, doesn't need to allocate the collection, and doesn't need to compute information like capture groups.

@stephentoub stephentoub added api-ready-for-review API is ready for review, it is NOT ready for implementation area-System.Text.RegularExpressions code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer labels Jan 9, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 9, 2025
Copy link
Contributor

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

@stephentoub stephentoub removed the untriaged New issue has not been triaged by the area owner label Jan 9, 2025
@ericstj ericstj added this to the 10.0.0 milestone Jan 13, 2025
@terrajobst
Copy link
Contributor

terrajobst commented Jan 21, 2025

Video

  • Looks good as proposed
    • Severity: Suggestion
    • Category: Performance

@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jan 21, 2025
@stephentoub stephentoub added the in-pr There is an active PR which will close this issue when it is merged label Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented area-System.Text.RegularExpressions code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants