Skip to content

Commit

Permalink
MSTEST0037 (proper assert analyzer): Don't report for user defined eq…
Browse files Browse the repository at this point in the history
…uality operators
  • Loading branch information
Youssef1313 committed Dec 27, 2024
1 parent 06eb7e4 commit f39fb30
Show file tree
Hide file tree
Showing 2 changed files with 378 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ private static bool IsIsNotNullPattern(IOperation operation, [NotNullWhen(true)]
private static bool IsEqualsNullBinaryOperator(IOperation operation, [NotNullWhen(true)] out SyntaxNode? expressionUnderTest)
{
if (operation is IBinaryOperation { OperatorKind: BinaryOperatorKind.Equals, RightOperand: { } rightOperand } binaryOperation &&
binaryOperation.OperatorMethod is not { MethodKind: MethodKind.UserDefinedOperator } &&
rightOperand.WalkDownConversion() is ILiteralOperation { ConstantValue: { HasValue: true, Value: null } })
{
expressionUnderTest = binaryOperation.LeftOperand.Syntax;
Expand All @@ -221,6 +222,7 @@ private static bool IsEqualsNullBinaryOperator(IOperation operation, [NotNullWhe
private static bool IsNotEqualsNullBinaryOperator(IOperation operation, [NotNullWhen(true)] out SyntaxNode? expressionUnderTest)
{
if (operation is IBinaryOperation { OperatorKind: BinaryOperatorKind.NotEquals, RightOperand: { } rightOperand } binaryOperation &&
binaryOperation.OperatorMethod is not { MethodKind: MethodKind.UserDefinedOperator } &&
rightOperand.WalkDownConversion() is ILiteralOperation { ConstantValue: { HasValue: true, Value: null } })
{
expressionUnderTest = binaryOperation.LeftOperand.Syntax;
Expand Down Expand Up @@ -253,7 +255,8 @@ private static EqualityCheckStatus RecognizeEqualityCheck(IOperation operation,
toBecomeActual = isPattern1.Value.Syntax;
return EqualityCheckStatus.Equals;
}
else if (operation is IBinaryOperation { OperatorKind: BinaryOperatorKind.Equals } binaryOperation1)
else if (operation is IBinaryOperation { OperatorKind: BinaryOperatorKind.Equals } binaryOperation1 &&
binaryOperation1.OperatorMethod is not { MethodKind: MethodKind.UserDefinedOperator })
{
// This is quite arbitrary. We can do extra checks to see which one (if any) looks like a "constant" and make it the expected.
toBecomeExpected = binaryOperation1.RightOperand.Syntax;
Expand All @@ -266,7 +269,8 @@ private static EqualityCheckStatus RecognizeEqualityCheck(IOperation operation,
toBecomeActual = isPattern2.Value.Syntax;
return EqualityCheckStatus.NotEquals;
}
else if (operation is IBinaryOperation { OperatorKind: BinaryOperatorKind.NotEquals } binaryOperation2)
else if (operation is IBinaryOperation { OperatorKind: BinaryOperatorKind.NotEquals } binaryOperation2 &&
binaryOperation2.OperatorMethod is not { MethodKind: MethodKind.UserDefinedOperator })
{
// This is quite arbitrary. We can do extra checks to see which one (if any) looks like a "constant" and make it the expected.
toBecomeExpected = binaryOperation2.RightOperand.Syntax;
Expand Down
Loading

0 comments on commit f39fb30

Please sign in to comment.