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.IsMatch guarding Regex.Match #111239

Open
stephentoub opened this issue Jan 9, 2025 · 1 comment
Open

[Analyzer]: Regex.IsMatch guarding Regex.Match #111239

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

In looking through various code bases, I've seen the following pattern repeatedly:

if (Regex.IsMatch(pattern))
{
    Match m = Regex.Match(pattern);
    ...
}

That just doubles the work involved when there is a match. It should instead be:

if (Regex.Match(pattern) is { Success: true } m)
{
    ...
}

or something similar. We should have an analyzer that flags these duplicative guards and a fixer that transforms into something like the latter.

@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