rspec/rules/S6001/rule.adoc
Arseniy Zaostrovnykh 7ca29f686f Force linebreaks
2021-02-02 15:02:10 +01:00

25 lines
1003 B
Plaintext

When a back reference in a regex refers to a capturing group that hasn't been defined yet (or at all), it can never be matched. Named back references throw a ``++PatternSyntaxException++`` in that case; numeric back references fail silently when they can't match, simply making the match fail.
When the group is defined before the back reference but on a different control path (like in ``++(.)|\1++`` for example), this also leads to a situation where the back reference can never match.
== Noncompliant Code Example
----
Pattern.compile("\\1(.)"); // Noncompliant, group 1 is defined after the back reference
Pattern.compile("(.)\\2"); // Noncompliant, group 2 isn't defined at all
Pattern.compile("(.)|\\1"); // Noncompliant, group 1 and the back reference are in different branches
Pattern.compile("(?<x>.)|\\k<x>"); // Noncompliant, group x and the back reference are in different branches
----
== Compliant Solution
----
Pattern.compile("(.)\\1");
Pattern.compile("(?<x>.)\\k<x>");
----