diff --git a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs index 09062dc78c249..ff82d4344aa68 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs @@ -140,8 +140,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context) } // At this point we'll be emitting code. Create a writer to hold it all. - var sw = new StringWriter(); - IndentedTextWriter writer = new(sw); + using StringWriter sw = new(); + using IndentedTextWriter writer = new(sw); // Add file headers and required usings. foreach (string header in s_headers)