public void IsEqualityComparerEqualsGeneric(string check, bool expected)
        {
            var testCode = @"
namespace RoslynSandbox
{
    using System.Collections.Generic;

    public class Foo
    {
        private int bar1;
        private int bar2;
        private string bar5;

        public bool Bar<T>(T arg1, T arg2)
        {
            return EqualityComparer<T>.Default.Equals(arg1, arg1);
        }

        public int? Bar1 => this.bar1;
    }
}";

            testCode = testCode.AssertReplace("EqualityComparer<T>.Default.Equals(arg1, arg1)", check);
            var syntaxTree    = CSharpSyntaxTree.ParseText(testCode);
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var invocation    = syntaxTree.FindInvocation(check);
            var arg0          = semanticModel.GetSymbolSafe(invocation.ArgumentList.Arguments[0].Expression, CancellationToken.None);
            var arg1          = semanticModel.GetSymbolSafe(invocation.ArgumentList.Arguments[1].Expression, CancellationToken.None);

            Assert.AreEqual(expected, Equality.IsEqualityComparerEquals(invocation, semanticModel, CancellationToken.None, arg0, arg1));
            Assert.AreEqual(expected, Equality.IsEqualityComparerEquals(invocation, semanticModel, CancellationToken.None, arg1, arg0));
        }
Exemple #2
0
        private static bool IsEqualsCheck(ExpressionSyntax expression, SemanticModel semanticModel, CancellationToken cancellationToken, IParameterSymbol value, ISymbol member)
        {
            var equals = expression as InvocationExpressionSyntax;

            if (equals == null)
            {
                return(false);
            }

            if (Equality.IsObjectEquals(equals, semanticModel, cancellationToken, value, member) ||
                Equality.IsInstanceEquals(equals, semanticModel, cancellationToken, value, member) ||
                Equality.IsInstanceEquals(equals, semanticModel, cancellationToken, member, value) ||
                Equality.IsReferenceEquals(equals, semanticModel, cancellationToken, value, member) ||
                Equality.IsEqualityComparerEquals(equals, semanticModel, cancellationToken, value, member) ||
                Equality.IsNullableEquals(equals, semanticModel, cancellationToken, value, member))
            {
                return(true);
            }

            return(false);
        }