diff --git a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs index 32ba674fccb5d7..f9c888a11c1a19 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs @@ -289,7 +289,6 @@ static string Literal(RegexOptions options) { // The options were formatted as an int, which means the runtime couldn't // produce a textual representation. So just output casting the value as an int. - Debug.Fail("This shouldn't happen, as we should only get to the point of emitting code if RegexOptions was valid."); return $"(RegexOptions)({(int)options})"; } diff --git a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs index 4e8b8c171fcd27..1a278912c62cc6 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs @@ -815,6 +815,97 @@ public partial class C await VerifyCS.VerifyCodeFixAsync(test, fixedCode); } + [Fact] + public async Task InvalidRegexOptions() + { + string test = @"using System.Text.RegularExpressions; + +public class A +{ + public void Foo() + { + Regex regex = [|new Regex(""pattern"", (RegexOptions)0x0800)|]; + } +} +"; + string fixedSource = @"using System.Text.RegularExpressions; + +public partial class A +{ + public void Foo() + { + Regex regex = MyRegex(); + } + + [RegexGenerator(""pattern"", (RegexOptions)2048)] + private static partial Regex MyRegex(); +} +"; + + await VerifyCS.VerifyCodeFixAsync(test, fixedSource); + } + + [Fact] + public async Task InvalidRegexOptions_LocalConstant() + { + string test = @"using System.Text.RegularExpressions; + +public class A +{ + public void Foo() + { + const RegexOptions MyOptions = (RegexOptions)0x0800; + Regex regex = [|new Regex(""pattern"", MyOptions)|]; + } +} +"; + string fixedSource = @"using System.Text.RegularExpressions; + +public partial class A +{ + public void Foo() + { + const RegexOptions MyOptions = (RegexOptions)0x0800; + Regex regex = MyRegex(); + } + + [RegexGenerator(""pattern"", (RegexOptions)2048)] + private static partial Regex MyRegex(); +} +"; + + await VerifyCS.VerifyCodeFixAsync(test, fixedSource); + } + + [Fact] + public async Task InvalidRegexOptions_Negative() + { + string test = @"using System.Text.RegularExpressions; + +public class A +{ + public void Foo() + { + Regex regex = [|new Regex(""pattern"", (RegexOptions)(-10000))|]; + } +} +"; + string fixedSource = @"using System.Text.RegularExpressions; + +public partial class A +{ + public void Foo() + { + Regex regex = MyRegex(); + } + + [RegexGenerator(""pattern"", (RegexOptions)(-10000))] + private static partial Regex MyRegex(); +} +"; + await VerifyCS.VerifyCodeFixAsync(test, fixedSource); + } + #region Test helpers private static string ConstructRegexInvocation(InvocationType invocationType, string pattern, string? options = null)