Delete erroneous simplifer annotation for regex fixer (#80126)
authorStephen Toub <stoub@microsoft.com>
Wed, 4 Jan 2023 12:57:57 +0000 (07:57 -0500)
committerGitHub <noreply@github.com>
Wed, 4 Jan 2023 12:57:57 +0000 (07:57 -0500)
* Delete erroneous simplifer annotation for regex fixer

* Fix a few test failures due to lack of simplification

src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs
src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs

index 442976d648b83404fbe1621021219bb1e2ee8ce7..e18a40c946cf1c39580110374c4e68da6a0580a9 100644 (file)
@@ -16,7 +16,6 @@ using Microsoft.CodeAnalysis.CSharp;
 using Microsoft.CodeAnalysis.CSharp.Syntax;
 using Microsoft.CodeAnalysis.Editing;
 using Microsoft.CodeAnalysis.Operations;
-using Microsoft.CodeAnalysis.Simplification;
 using Microsoft.CodeAnalysis.Text;
 
 namespace System.Text.RegularExpressions.Generator
@@ -129,7 +128,7 @@ namespace System.Text.RegularExpressions.Generator
                     if (!typeDeclaration.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)))
                     {
                         typesModified++;
-                        return typeDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword)).WithAdditionalAnnotations(Simplifier.Annotation);
+                        return typeDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword));
                     }
 
                     return typeDeclaration;
index 9dafdf410201aa16affc7d1ce9958572057094f8..2de03c9b3f496dfa886433a112666a8f31bba4fd 100644 (file)
@@ -754,6 +754,37 @@ partial class Program
         Regex r = MyRegex();
     }
 
+    [GeneratedRegex(""a|b"", RegexOptions.None)]
+    private static partial Regex MyRegex();
+}";
+
+            await VerifyCS.VerifyCodeFixAsync(test, fixedSource);
+        }
+
+        [Fact]
+        public async Task CodeFixerDoesNotSimplifyStyle()
+        {
+            string test = @"using System.Text.RegularExpressions;
+
+class Program
+{
+    static void Main()
+    {
+        int i = (4 - 4); // this shouldn't be changed by fixer
+        Regex r = [|new Regex(options: RegexOptions.None, pattern: ""a|b"")|];
+    }
+}";
+
+            string fixedSource = @"using System.Text.RegularExpressions;
+
+partial class Program
+{
+    static void Main()
+    {
+        int i = (4 - 4); // this shouldn't be changed by fixer
+        Regex r = MyRegex();
+    }
+
     [GeneratedRegex(""a|b"", RegexOptions.None)]
     private static partial Regex MyRegex();
 }";
@@ -942,7 +973,7 @@ public partial class A
         Regex regex = MyRegex();
     }
 
-    [GeneratedRegex(""pattern"", (RegexOptions)2048)]
+    [GeneratedRegex(""pattern"", (RegexOptions)(2048))]
     private static partial Regex MyRegex();
 }
 ";
@@ -974,7 +1005,7 @@ public partial class A
         Regex regex = MyRegex();
     }
 
-    [GeneratedRegex(""pattern"", (RegexOptions)2048)]
+    [GeneratedRegex(""pattern"", (RegexOptions)(2048))]
     private static partial Regex MyRegex();
 }
 ";