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:
parent
67aba10083
commit
a5a9904a80
2
rules/S6397/metadata.json
Normal file
2
rules/S6397/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
18
rules/S6397/php/metadata.json
Normal file
18
rules/S6397/php/metadata.json
Normal 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
35
rules/S6397/php/rule.adoc
Normal 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[]
|
Loading…
x
Reference in New Issue
Block a user