Skip to content

Debug.Fail in UpgradeToRegexGeneratorCodeFixer is reachable #72085

@Youssef1313

Description

@Youssef1313

Description

The assumption here is incorrect:

// 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.");

This path is reachable.

Reproduction Steps

Tests extracted from #71264:

        [Fact]
        public async Task InvalidRegexOptions()
        {
            string test = @"using System.Text.RegularExpressions;

public class A
{
    public partial class B
    {
        public class C
        {
            public partial class D
            {
                public void Foo()
                {
                    Regex regex = [|new Regex(""pattern"", (RegexOptions)0x0800)|];
                }
            }
        }
    }
}
";
            string fixedSource = @"using System.Text.RegularExpressions;

public partial class A
{
    public partial class B
    {
        public partial class C
        {
            public partial class D
            {
                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_Negative()
        {
            string test = @"using System.Text.RegularExpressions;

public class A
{
    public partial class B
    {
        public class C
        {
            public partial class D
            {
                public void Foo()
                {
                    Regex regex = [|new Regex(""pattern"", (RegexOptions)(-10000))|];
                }
            }
        }
    }
}
";
            string fixedSource = @"using System.Text.RegularExpressions;

public partial class A
{
    public partial class B
    {
        public partial class C
        {
            public partial class D
            {
                public void Foo()
                {
                    Regex regex = MyRegex();
                }

                [RegexGenerator(""pattern"", (RegexOptions)(-10000))]
                private static partial Regex MyRegex();
            }
        }
    }
}
";
            await new VerifyCS.Test(null, usePreviewLanguageVersion: true, 1)
            {
                TestCode = test,
                FixedCode = fixedSource,
                CodeActionValidationMode = CodeActionValidationMode.None,
            }.RunAsync();
        }

Expected behavior

Tests above should pass.

Actual behavior

Debug.Fail is hit.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions