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 · 1 comment
Open

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

stephentoub opened this issue Jan 9, 2025 · 1 comment
Labels
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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
Projects
None yet
Development

No branches or pull requests

1 participant