-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Text.RegularExpressionsblockingMarks issues that we want to fast track in order to unblock other important workMarks issues that we want to fast track in order to unblock other important workcode-analyzerMarks an issue that suggests a Roslyn analyzerMarks an issue that suggests a Roslyn analyzer
Milestone
Description
Suggested severity: Info
Suggested category: Performance
For new Regex(...) and Regex.StaticMethod(...) involving patterns, options, and timeouts known at compile time, we should provide an analyzer and fixer that will transform the regex into its corresponding RegexGenerator form, including fixing up the call sites.
Flag
- Use of Regex constructor or static method with inline literals like:
// Flag object creation invocations
Regex myRegex = new Regex("ab|cd", RegexOptions.None, TimeSpan.FromSeconds(2));
// Flag static method invocations
var match = Regex.Match(input, "ab|cd", RegexOptions.None, TimeSpan.FromSeconds(2));- Use of Regex constructor or static method when using constant values like:
const string pattern = "ab|cd";
const RegexOptions options = RegexOptions.None;
Regex myRegex = new Regex(pattern, options, TimeSpan.FromSeconds(2));
var match = Regex.Match(input, pattern, options, TimeSpan.FromSeconds(2));Don't Flag
- Use of non-constant variables as parameters like:
public void Foo(RegexOptions parameterOptions)
{
string nonConstantPattern = "ab|cd";
Regex myRegex = new Regex(nonConstantPattern , parameterOptions, TimeSpan.FromSeconds(2));
var match = Regex.Match(input, nonConstantPattern , parameterOptions, TimeSpan.FromSeconds(2));
}- Options contain NonBacktracking (since this option is not supported by source generator yet) like:
Regex myRegex = new Regex("ab|cd", RegexOptions.NonBacktracking);Fixer
The fixer will mainly do three things:
- Add
partialkeyword (if it's not there already) to the type (or types for nested types) which contains the invocation that generated the diagnostic - Declare a new private static partial method with the
RegexGeneratorattribute and parameters so the source generator generates the Regex type. - Change the invocation that produced the diagnostic to use the new static partial method instead.
jnyrup and martincostello
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Text.RegularExpressionsblockingMarks issues that we want to fast track in order to unblock other important workMarks issues that we want to fast track in order to unblock other important workcode-analyzerMarks an issue that suggests a Roslyn analyzerMarks an issue that suggests a Roslyn analyzer