Sonarqube thinks `ReferenceEquals(true,true);` is false

        if (ReferenceEquals(actual, expected)) //check if are the same or both null
        {
            return true;
        }
        if (actual == null || expected == null) //cheks if any of them is null
        {
            return false;
        }

        //At this point neither actual or expected can be null.
        if (actual.Count != expected.Count)

SonarQube complains about ‘actual’ or ‘expected’ could be null here in the last if, which is not possible.

According to MSDN:
https://docs.microsoft.com/en-us/dotnet/api/system.object.referenceequals?view=netframework-4.7.1

 public static bool ReferenceEquals (object objA, object objB);

true if objA is the same instance as objB or if both are null ; otherwise, false .

1 Like

Thanks @theDiver for the reproducer. Unfortunately, I cannot repro.


    class TestList
    {
        private bool Equals(List<string> actual = null, List<string> expected = null)
        {
            actual = null;
            expected = null;
            if (ReferenceEquals(actual, expected)) //check if are the same or both null
            {
                return true;
            }
            if (actual == null || expected == null) //cheks if any of them is null
            {
                return false;
            }

            //At this point neither actual or expected can be null.
            return (actual.Count != expected.Count);
        }
    }

This does not raise an issue…