2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
Why use named groups only to never use any of them later on in the code?
|
|
|
|
|
|
|
|
This rule raises issues every time named groups are:
|
|
|
|
|
|
|
|
* referenced while not defined;
|
|
|
|
* defined but called elsewhere in the code by their number instead.
|
|
|
|
|
2023-03-29 10:41:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2023-03-29 10:41:03 +02:00
|
|
|
|
|
|
|
[source,python]
|
|
|
|
----
|
|
|
|
import re
|
|
|
|
|
|
|
|
def foo():
|
|
|
|
pattern = re.compile(r"(?P<a>.)")
|
|
|
|
matches = pattern.match("abc")
|
|
|
|
g1 = matches.group("b") # Noncompliant - group "b" is not defined
|
|
|
|
g2 = matches.group(1) # Noncompliant - Directly use 'a' instead of its group number.
|
|
|
|
----
|
|
|
|
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2023-03-29 10:41:03 +02:00
|
|
|
|
|
|
|
[source,python]
|
|
|
|
----
|
|
|
|
import re
|
|
|
|
|
|
|
|
def foo():
|
|
|
|
pattern = re.compile(r"(?P<a>.)")
|
|
|
|
matches = pattern.match("abc")
|
|
|
|
g = matches.group("a")
|
|
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
* In case there is no group with name:
|
|
|
|
** There is no group named 'S' in the regular expression.
|
|
|
|
* In case number access to named group
|
|
|
|
** Directly use 'S' instead of its group number.
|
|
|
|
|
|
|
|
|
|
|
|
=== Highlighting
|
|
|
|
|
|
|
|
* primary: the group number or group name
|
|
|
|
** secondary: named groups
|
2023-03-29 10:41:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|