Add CA2027 analyzer to detect redundant Regex.IsMatch guard before Regex.Match#51214
Closed
Add CA2027 analyzer to detect redundant Regex.IsMatch guard before Regex.Match#51214
Conversation
Copilot
AI
changed the title
[WIP] Add analyzer and fixer for redundant Regex.IsMatch usage
Add CA2027 analyzer to detect redundant Regex.IsMatch guard before Regex.Match
Oct 12, 2025
stephentoub
reviewed
Oct 12, 2025
Member
|
@copilot, there are build failures: |
Contributor
Author
Fixed by regenerating the auto-generated documentation files. The files now contain the updated CA2027 descriptions with the corrected resource strings. |
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR implements analyzer CA2027 to detect redundant Regex.IsMatch guard patterns before Regex.Match calls. The analyzer identifies cases where developers unnecessarily check Regex.IsMatch before calling Regex.Match with the same arguments, which duplicates the matching operation and impacts performance.
Key changes include:
- New analyzer that detects both standard guard patterns (
if (IsMatch) { Match... }) and inverted early-return patterns (if (!IsMatch) { return; } Match...) - Language-specific code fixers for C# and Visual Basic that remove redundant guards
- Comprehensive test coverage with 14 test cases covering various scenarios including dataflow analysis to prevent false positives
Reviewed Changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| AvoidRedundantRegexIsMatchBeforeMatch.cs | Core analyzer implementation with pattern detection and dataflow analysis |
| CSharpAvoidRedundantRegexIsMatchBeforeMatch.Fixer.cs | C# code fixer that transforms guard patterns to use Match().Success |
| BasicAvoidRedundantRegexIsMatchBeforeMatch.Fixer.vb | Visual Basic code fixer for removing redundant guards |
| AvoidRedundantRegexIsMatchBeforeMatchTests.cs | Comprehensive test suite covering positive/negative cases and edge scenarios |
| MicrosoftNetCoreAnalyzersResources.resx | Resource strings for analyzer messages and descriptions |
| DiagnosticCategoryAndIdRanges.txt | Updated rule ID range to include CA2027 |
| AnalyzerReleases.Unshipped.md | Added CA2027 to unshipped analyzer releases |
| Multiple .xlf files | Localization files with new resource strings marked for translation |
| Microsoft.CodeAnalysis.NetAnalyzers.md/.sarif | Auto-generated documentation files |
stephentoub
reviewed
Oct 13, 2025
stephentoub
reviewed
Oct 15, 2025
stephentoub
reviewed
Oct 16, 2025
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…ignments, update resource strings Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…ode review feedback Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…t/decrement checks Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
d8cfa91 to
133ed57
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implementation Complete: CA2027 Analyzer for Redundant Regex.IsMatch Guard
Summary
Successfully implemented analyzer CA2027 with all code review feedback addressed.
Problem Pattern Detected
Latest Changes (This Commit)
Code Simplifications:
FindMatchCallInBranchandFindMatchCallRecursiveinto a single recursive method - the separate wrapper was unnecessaryHasAssignmentToSymbol- not relevant for string arguments used in Regex methodsBenefits:
Test Results:
✅ All 26 tests passing
✅ Zero regressions
✅ All feedback addressed
Original prompt
Fixes dotnet/runtime#111239
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.