S5856: Add C# and VB.NET (#1528)
This commit is contained in:
parent
51b0652467
commit
a8c0d761a8
1
rules/S5856/csharp/metadata.json
Normal file
1
rules/S5856/csharp/metadata.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{}
|
43
rules/S5856/csharp/rule.adoc
Normal file
43
rules/S5856/csharp/rule.adoc
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
include::../description.adoc[]
|
||||||
|
|
||||||
|
Negative lookahead and negative lookbehind groups cannot be combined with `RegexOptions.NonBacktracking`. Such combination would throw an exception during runtime.
|
||||||
|
|
||||||
|
== Noncompliant Code Example
|
||||||
|
|
||||||
|
[source,csharp]
|
||||||
|
----
|
||||||
|
public void DoSomething(string input)
|
||||||
|
{
|
||||||
|
var regex = new Regex("[A"); // Noncompliant
|
||||||
|
var match = Regex.Match(input, "[A"); // Noncompliant
|
||||||
|
var matches = Regex.Matches(input, "[A"); // Noncompliant
|
||||||
|
var replace = Regex.Replace(input, "[A", "replacement"); // Noncompliant
|
||||||
|
var split = Regex.Split(input, "[A"); // Noncompliant
|
||||||
|
if(Regex.IsMatch(input, "[A")) // Noncompliant
|
||||||
|
{
|
||||||
|
}
|
||||||
|
var negativeLookahead = new Regex("a(?!b)", RegexOptions.NonBacktracking); // Noncompliant
|
||||||
|
var negativeLookbehind = new Regex("(?<!a)b", RegexOptions.NonBacktracking); // Noncompliant
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
== Compliant Solution
|
||||||
|
|
||||||
|
[source,csharp]
|
||||||
|
----
|
||||||
|
public void DoSomething(string input)
|
||||||
|
{
|
||||||
|
var regex = new Regex("[A-Z]");
|
||||||
|
var match = Regex.Match(input, "[A-Z]");
|
||||||
|
var matches = Regex.Matches(input, "[A-Z]");
|
||||||
|
var replace = Regex.Replace(input, "[A-Z]", "replacement");
|
||||||
|
var split = Regex.Split(input, "[A-Z]");
|
||||||
|
if(Regex.IsMatch(input, "[A-Z]"))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
var negativeLookahead = new Regex("a(?!b)");
|
||||||
|
var negativeLookbehind = new Regex("(?<!a)b");
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
include::../rspecator.adoc[]
|
@ -25,14 +25,4 @@ str.replace("([", "{");
|
|||||||
str.matches("(\\w+)-(\\d+)");
|
str.matches("(\\w+)-(\\d+)");
|
||||||
----
|
----
|
||||||
|
|
||||||
ifdef::env-github,rspecator-view[]
|
include::../rspecator.adoc[]
|
||||||
|
|
||||||
'''
|
|
||||||
== Implementation Specification
|
|
||||||
(visible only on this page)
|
|
||||||
|
|
||||||
include::../message.adoc[]
|
|
||||||
|
|
||||||
include::../highlighting.adoc[]
|
|
||||||
|
|
||||||
endif::env-github,rspecator-view[]
|
|
||||||
|
@ -19,3 +19,4 @@ str.match("\\(\\[");
|
|||||||
str.replace("([", "{");
|
str.replace("([", "{");
|
||||||
----
|
----
|
||||||
|
|
||||||
|
include::../rspecator.adoc[]
|
||||||
|
@ -25,14 +25,4 @@ Regex("([", RegexOption.LITERAL)
|
|||||||
"([".toRegex(RegexOption.LITERAL)
|
"([".toRegex(RegexOption.LITERAL)
|
||||||
----
|
----
|
||||||
|
|
||||||
ifdef::env-github,rspecator-view[]
|
include::../rspecator.adoc[]
|
||||||
|
|
||||||
'''
|
|
||||||
== Implementation Specification
|
|
||||||
(visible only on this page)
|
|
||||||
|
|
||||||
include::../message.adoc[]
|
|
||||||
|
|
||||||
include::../highlighting.adoc[]
|
|
||||||
|
|
||||||
endif::env-github,rspecator-view[]
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
=== Message
|
=== Message
|
||||||
|
|
||||||
Fix this syntax error inside this regex
|
Fix the syntax error inside this regex
|
||||||
|
|
||||||
|
@ -22,3 +22,4 @@ str_replace("([", "{", $input);
|
|||||||
preg_replace("/(\\w+)-(\\d+)/", "1234", $input);
|
preg_replace("/(\\w+)-(\\d+)/", "1234", $input);
|
||||||
----
|
----
|
||||||
|
|
||||||
|
include::../rspecator.adoc[]
|
||||||
|
@ -22,3 +22,4 @@ input.replace("([", "{")
|
|||||||
re.compile(r"(\w+)-(\d+)")
|
re.compile(r"(\w+)-(\d+)")
|
||||||
----
|
----
|
||||||
|
|
||||||
|
include::../rspecator.adoc[]
|
||||||
|
11
rules/S5856/rspecator.adoc
Normal file
11
rules/S5856/rspecator.adoc
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
ifdef::env-github,rspecator-view[]
|
||||||
|
|
||||||
|
'''
|
||||||
|
== Implementation Specification
|
||||||
|
(visible only on this page)
|
||||||
|
|
||||||
|
include::message.adoc[]
|
||||||
|
|
||||||
|
include::highlighting.adoc[]
|
||||||
|
|
||||||
|
endif::env-github,rspecator-view[]
|
1
rules/S5856/vbnet/metadata.json
Normal file
1
rules/S5856/vbnet/metadata.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{}
|
39
rules/S5856/vbnet/rule.adoc
Normal file
39
rules/S5856/vbnet/rule.adoc
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
include::../description.adoc[]
|
||||||
|
|
||||||
|
Negative lookahead and negative lookbehind groups cannot be combined with `RegexOptions.NonBacktracking`. Such combination would throw an exception during runtime.
|
||||||
|
|
||||||
|
== Noncompliant Code Example
|
||||||
|
|
||||||
|
[source,vbnet]
|
||||||
|
----
|
||||||
|
Public Sub DoSomething(Input As String)
|
||||||
|
Dim Rx As New Regex("[A") ' Noncompliant
|
||||||
|
Dim Match = Regex.Match(Input, "[A") ' Noncompliant
|
||||||
|
Dim Matches = Regex.Matches(Input, "[A") ' Noncompliant
|
||||||
|
Dim Replace = Regex.Replace(Input, "[A", "replacement") ' Noncompliant
|
||||||
|
Dim Split = Regex.Split(Input, "[A") ' Noncompliant
|
||||||
|
If (Regex.IsMatch(Input, "[A")) Then ' Noncompliant
|
||||||
|
End If
|
||||||
|
Dim NegativeLookahead As New Regex("a(?!b)", RegexOptions.NonBacktracking) ' Noncompliant
|
||||||
|
Dim NegativeLookbehind As New Regex("(?<!a)b", RegexOptions.NonBacktracking) ' Noncompliant
|
||||||
|
End Sub
|
||||||
|
----
|
||||||
|
|
||||||
|
== Compliant Solution
|
||||||
|
|
||||||
|
[source,vbnet]
|
||||||
|
----
|
||||||
|
Public Sub DoSomething(Input As String)
|
||||||
|
Dim Rx As New Regex("[A-Z]")
|
||||||
|
Dim Match = Regex.Match(Input, "[A-Z]")
|
||||||
|
Dim Matches = Regex.Matches(Input, "[A-Z]")
|
||||||
|
Dim Replace = Regex.Replace(Input, "[A-Z]", "replacement")
|
||||||
|
Dim Split = Regex.Split(Input, "[A-Z]")
|
||||||
|
If (Regex.IsMatch(Input, "[A-Z]")) Then
|
||||||
|
End If
|
||||||
|
Dim NegativeLookahead As New Regex("a(?!b)")
|
||||||
|
Dim NegativeLookbehind As New Regex("(?<!a)b")
|
||||||
|
End Sub
|
||||||
|
----
|
||||||
|
|
||||||
|
include::../rspecator.adoc[]
|
Loading…
x
Reference in New Issue
Block a user