rspec/rules/S6001/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

45 lines
1.3 KiB
Plaintext

== Why is this an issue?
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
[source,java]
----
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
[source,java]
----
Pattern.compile("(.)\\1");
Pattern.compile("(?<x>.)\\k<x>");
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]