rspec/rules/S5840/java/rule.adoc

56 lines
1.1 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Using certain features of regular expressions, it is possible to create regular expressions that can never match or contain subpatterns that can never match. Since a pattern or sub-pattern that can never match any input is pointless, this is a sign that the pattern does not work as intended and needs to be fixed.
This rule finds some such regular expressions and subpatterns, specifically ones that meet one of the following conditions:
* Beginning- and end-of-line/input boundaries appearing in a position where they can never match (e.g. an end-of-input marker being followed by other characters)
* A back reference refers to a capturing group that will never be matched before the back reference
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
=== Boundaries
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
$[a-z]*^
----
=== Backreference
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
\1(.)
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
=== Boundaries
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
^[a-z]*$
----
=== Backreference
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
(.)\1
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]