Skip to content

"Convert to GeneratedRegexAttribute" mangles Constants #109784

Closed
@TonyValenti

Description

@TonyValenti

Description

When using the "Convert to GeneratedRegexAttribute" fixer in VS, it mangles constant RegexOptions.

Reproduction Steps

Use this code:

        const RegexOptions MyOptions = RegexOptions.Compiled | RegexOptions.IgnoreCase;

        static Regex RX = new("asdf", MyOptions);

And apply the fixer. You now have the following:

        const RegexOptions MyOptions = RegexOptions.Compiled | RegexOptions.IgnoreCase;

        static Regex RX = MyRegex();
        
        [GeneratedRegex("asdf", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en-US")]
        private static partial Regex MyRegex();

You'll notice that the constant, MyOptions, is not used in the generated regex but is now expanded.

Expected behavior

I should get this code:

        const RegexOptions MyOptions = RegexOptions.Compiled | RegexOptions.IgnoreCase;

        static Regex RX = MyRegex();
        
        [GeneratedRegex("asdf", MyOptions, "en-US")]
        private static partial Regex MyRegex();

Actual behavior

You'll notice that the constant, MyOptions, is not used in the generated regex but is now expanded.

Regression?

No response

Known Workarounds

Manually edit the "Fixed" code.

Configuration

VS 17.11.5

Other information

This is what I'm triggering:
Image

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions