Skip to content

Commit cc0daca

Browse files
committed
C#: Support user defined compound assignment operators.
1 parent 8d2c5c2 commit cc0daca

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Util/SymbolExtensions.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,24 @@ public static bool TryGetOperatorSymbol(this ISymbol symbol, out string operator
5858
{
5959
var methodName = symbol.GetName(useMetadataName: false);
6060

61+
// Most common use-case.
6162
if (methodToOperator.TryGetValue(methodName, out operatorName!))
6263
return true;
6364

64-
var match = CheckedRegex().Match(methodName);
65-
if (match.Success && methodToOperator.TryGetValue($"op_{match.Groups[1]}", out var uncheckedName))
65+
// Attempt to parse using a regexp.
66+
var match = OperatorRegex().Match(methodName);
67+
if (match.Success && methodToOperator.TryGetValue($"op_{match.Groups[2]}", out var rawOperatorName))
6668
{
67-
operatorName = $"checked {uncheckedName}";
69+
var prefix = match.Groups[1].Success ? "checked " : "";
70+
var postfix = match.Groups[3].Success ? "=" : "";
71+
operatorName = $"{prefix}{rawOperatorName}{postfix}";
6872
return true;
6973
}
74+
7075
return false;
7176
}
7277

73-
[GeneratedRegex("^op_Checked(.*)$")]
74-
private static partial Regex CheckedRegex();
78+
[GeneratedRegex("^op_(Checked)?(.*?)(Assignment)?$")]
79+
private static partial Regex OperatorRegex();
7580
}
7681
}

0 commit comments

Comments
 (0)