Create rule S6397: Character classes in regular expressions should not contain only one character (#648)

Co-authored-by: karim-ouerghemmi-sonarsource <karim-ouerghemmi-sonarsource@users.noreply.github.com>
This commit is contained in:
github-actions[bot] 2022-02-01 11:58:45 +01:00 committed by GitHub
parent 67aba10083
commit a5a9904a80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,2 @@
{
}

View File

@ -0,0 +1,18 @@
{
"title": "Character classes in regular expressions should not contain only one character",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"regex"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6397",
"sqKey": "S6397",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown"
}

35
rules/S6397/php/rule.adoc Normal file
View File

@ -0,0 +1,35 @@
Character classes in regular expressions are a convenient way to match one of several possible characters by listing the allowed characters or ranges of characters. If a character class contains only one character, the effect is the same as just writing the character without a character class.
Thus, having only one character in a character class is usually a simple oversight that remained after removing other characters of the class.
== Noncompliant Code Example
----
/a[b]c/
----
== Compliant Solution
----
/abc/
----
== Exceptions
This rule does not raise when the character inside the class is a metacharacter. This notation is sometimes used to avoid escaping (e.g., ``++[.]{3}++`` to match three dots).
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace this character class by the character itself.
=== Highlighting
The character class.
endif::env-github,rspecator-view[]