-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add CA2027 analyzer to detect redundant Regex.IsMatch guard before Regex.Match #51214
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
base: main
Are you sure you want to change the base?
Conversation
...odeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...Analyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidRedundantRegexIsMatchBeforeMatch.Fixer.cs
Outdated
Show resolved
Hide resolved
...is.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidRedundantRegexIsMatchBeforeMatch.cs
Show resolved
Hide resolved
...is.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidRedundantRegexIsMatchBeforeMatch.cs
Outdated
Show resolved
Hide resolved
@copilot, there are build failures:
|
Fixed by regenerating the auto-generated documentation files. The files now contain the updated CA2027 descriptions with the corrected resource strings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
...zers/Microsoft.NetCore.Analyzers/Runtime/BasicAvoidRedundantRegexIsMatchBeforeMatch.Fixer.vb
Outdated
Show resolved
Hide resolved
...is.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidRedundantRegexIsMatchBeforeMatch.cs
Outdated
Show resolved
Hide resolved
...is.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidRedundantRegexIsMatchBeforeMatch.cs
Outdated
Show resolved
Hide resolved
...is.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidRedundantRegexIsMatchBeforeMatch.cs
Outdated
Show resolved
Hide resolved
...is.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidRedundantRegexIsMatchBeforeMatch.cs
Outdated
Show resolved
Hide resolved
....UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidRedundantRegexIsMatchBeforeMatchTests.cs
Show resolved
Hide resolved
...ers/Microsoft.NetCore.Analyzers/Runtime/CSharpAvoidRedundantRegexIsMatchBeforeMatch.Fixer.cs
Outdated
Show resolved
Hide resolved
...ers/Microsoft.NetCore.Analyzers/Runtime/CSharpAvoidRedundantRegexIsMatchBeforeMatch.Fixer.cs
Outdated
Show resolved
Hide resolved
...ers/Microsoft.NetCore.Analyzers/Runtime/CSharpAvoidRedundantRegexIsMatchBeforeMatch.Fixer.cs
Outdated
Show resolved
Hide resolved
...is.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidRedundantRegexIsMatchBeforeMatch.cs
Outdated
Show resolved
Hide resolved
...is.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidRedundantRegexIsMatchBeforeMatch.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
…ignments, update resource strings Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
…ode review feedback Co-authored-by: stephentoub <[email protected]>
…t/decrement checks Co-authored-by: stephentoub <[email protected]>
d8cfa91
to
133ed57
Compare
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:
FindMatchCallInBranch
andFindMatchCallRecursive
into 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.