Repository: pre-commit/pre-commit

Issue:
More on warnings for regular expressions
Hi!

As briefly discussed here:
https://github.com/pre-commit/pre-commit/pull/2053#issuecomment-983436239

`pre-commit` could detect the `[\\/]` regular expression as well.
As of now (pre-commit 2.16.0), such a regex does not trigger any warnings.

Besides, IIRC, the regex `[\/]`, currently detected, matches the single character `/` (not `/` and `\`), so I think it shouldn't be mentioned as an erroneous way for matching both kinds of slashes.
(see https://regex101.com/r/FRPiLZ/1)

Thanks!


Discussion:

Resolving patch:
diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py
--- a/pre_commit/clientlib.py
+++ b/pre_commit/clientlib.py
@@ -144,18 +144,13 @@ def check(self, dct: Dict[str, Any]) -> None:
                 f"regex, not a glob -- matching '/*' probably isn't what you "
                 f'want here',
             )
-        if r'[\/]' in dct.get(self.key, ''):
-            logger.warning(
-                fr'pre-commit normalizes slashes in the {self.key!r} field '
-                fr'in hook {dct.get("id")!r} to forward slashes, so you '
-                fr'can use / instead of [\/]',
-            )
-        if r'[/\\]' in dct.get(self.key, ''):
-            logger.warning(
-                fr'pre-commit normalizes slashes in the {self.key!r} field '
-                fr'in hook {dct.get("id")!r} to forward slashes, so you '
-                fr'can use / instead of [/\\]',
-            )
+        for fwd_slash_re in (r'[\\/]', r'[\/]', r'[/\\]'):
+            if fwd_slash_re in dct.get(self.key, ''):
+                logger.warning(
+                    fr'pre-commit normalizes slashes in the {self.key!r} '
+                    fr'field in hook {dct.get("id")!r} to forward slashes, '
+                    fr'so you can use / instead of {fwd_slash_re}',
+                )
 
 
 class OptionalSensibleRegexAtTop(cfgv.OptionalNoDefault):
@@ -167,18 +162,13 @@ def check(self, dct: Dict[str, Any]) -> None:
                 f'The top-level {self.key!r} field is a regex, not a glob -- '
                 f"matching '/*' probably isn't what you want here",
             )
-        if r'[\/]' in dct.get(self.key, ''):
-            logger.warning(
-                fr'pre-commit normalizes the slashes in the top-level '
-                fr'{self.key!r} field to forward slashes, so you can use / '
-                fr'instead of [\/]',
-            )
-        if r'[/\\]' in dct.get(self.key, ''):
-            logger.warning(
-                fr'pre-commit normalizes the slashes in the top-level '
-                fr'{self.key!r} field to forward slashes, so you can use / '
-                fr'instead of [/\\]',
-            )
+        for fwd_slash_re in (r'[\\/]', r'[\/]', r'[/\\]'):
+            if fwd_slash_re in dct.get(self.key, ''):
+                logger.warning(
+                    fr'pre-commit normalizes the slashes in the top-level '
+                    fr'{self.key!r} field to forward slashes, so you '
+                    fr'can use / instead of {fwd_slash_re}',
+                )
 
 
 class MigrateShaToRev:


Test patch:
diff --git a/tests/clientlib_test.py b/tests/clientlib_test.py
--- a/tests/clientlib_test.py
+++ b/tests/clientlib_test.py
@@ -265,6 +265,11 @@ def test_warn_mutable_rev_conditional():
             r"pre-commit normalizes slashes in the 'files' field in hook "
             r"'flake8' to forward slashes, so you can use / instead of [/\\]",
         ),
+        (
+            r'dir[\\/].*\.py',
+            r"pre-commit normalizes slashes in the 'files' field in hook "
+            r"'flake8' to forward slashes, so you can use / instead of [\\/]",
+        ),
     ),
 )
 def test_validate_optional_sensible_regex_at_hook(caplog, regex, warning):
@@ -295,6 +300,11 @@ def test_validate_optional_sensible_regex_at_hook(caplog, regex, warning):
             r"pre-commit normalizes the slashes in the top-level 'files' "
             r'field to forward slashes, so you can use / instead of [/\\]',
         ),
+        (
+            r'dir[\\/].*\.py',
+            r"pre-commit normalizes the slashes in the top-level 'files' "
+            r'field to forward slashes, so you can use / instead of [\\/]',
+        ),
     ),
 )
 def test_validate_optional_sensible_regex_at_top_level(caplog, regex, warning):