diff --git a/rules/S6323/description.adoc b/rules/S6323/description.adoc new file mode 100644 index 0000000000..7267844a89 --- /dev/null +++ b/rules/S6323/description.adoc @@ -0,0 +1 @@ +Alternation is used to match a single regular expression out of several possible regular expressions. If one of the alternatives is empty it would match any input, which is most probably a mistake. \ No newline at end of file diff --git a/rules/S6323/javascript/metadata.json b/rules/S6323/javascript/metadata.json index a218651e26..2c63c08510 100644 --- a/rules/S6323/javascript/metadata.json +++ b/rules/S6323/javascript/metadata.json @@ -1,20 +1,2 @@ { - "title": "Alternation in regular expressions should not contain empty alternatives", - "type": "BUG", - "status": "ready", - "remediation": { - "func": "Constant\/Issue", - "constantCost": "5min" - }, - "tags": [ - "regex" - ], - "defaultSeverity": "Major", - "ruleSpecification": "RSPEC-6323", - "sqKey": "S6323", - "scope": "Main", - "defaultQualityProfiles": [ - "Sonar way" - ], - "quickfix": "unknown" } diff --git a/rules/S6323/javascript/rule.adoc b/rules/S6323/javascript/rule.adoc index 3ed2dd26ff..c2011f6a44 100644 --- a/rules/S6323/javascript/rule.adoc +++ b/rules/S6323/javascript/rule.adoc @@ -1,4 +1,4 @@ -Alternation is used to match a single regular expression out of several possible regular expressions. If one of the alternatives is empty it would match any input, which is most probably a mistake. +include::../description.adoc[] == Noncompliant Code Example ---- diff --git a/rules/S6323/metadata.json b/rules/S6323/metadata.json index 2c63c08510..89746eed22 100644 --- a/rules/S6323/metadata.json +++ b/rules/S6323/metadata.json @@ -1,2 +1,21 @@ { -} + "title": "Alternation in regular expressions should not contain empty alternatives", + "type": "BUG", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "regex" + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-6323", + "sqKey": "S6323", + "scope": "Main", + "defaultQualityProfiles": [ + "Sonar way" + ], + "quickfix": "unknown" + } + \ No newline at end of file diff --git a/rules/S6323/php/metadata.json b/rules/S6323/php/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S6323/php/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S6323/php/rule.adoc b/rules/S6323/php/rule.adoc new file mode 100644 index 0000000000..ab5b108385 --- /dev/null +++ b/rules/S6323/php/rule.adoc @@ -0,0 +1,38 @@ +include::../description.adoc[] + +== Noncompliant Code Example +---- +preg_match("/Jack|Peter|/", "John"); // Noncompliant - returns 1 +preg_match("/Jack||Peter/", "John"); // Noncompliant - returns 1 +---- +== Compliant Solution +---- +preg_match("/Jack|Peter/", "John"); // returns 0 +---- + +== Exceptions + +One could use an empty alternation to make a regular expression group optional. Rule will not report on such cases. + +---- +preg_match("/mandatory(-optional|)/", "mandatory"); // returns 1 +preg_match("/mandatory(-optional|)/", "mandatory-optional"); // returns 1 +---- + +ifdef::env-github,rspecator-view[] + +''' +== Implementation Specification +(visible only on this page) + +== Message + +Remove this empty alternative. + +== Highlighting + +The | should be highlighted. + +''' + +endif::env-github,rspecator-view[]