Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RCS1132 false positive (and conflict with CS8851) #744

Closed
eduherminio opened this issue Nov 11, 2020 · 3 comments · Fixed by #1015
Closed

RCS1132 false positive (and conflict with CS8851) #744

eduherminio opened this issue Nov 11, 2020 · 3 comments · Fixed by #1015

Comments

@eduherminio
Copy link
Member

Product and Version Used:

  • Roslynator 2019 3.0.1 (VS 2019 extension)
  • .NET 5.0

Steps to Reproduce:

public record Foo(int A)
{
    public virtual bool Equals(Foo? other)
    {
        if (other is null)
        {
            return false;
        }

        return A.Equals(other.A);
    }

    public override int GetHashCode() => A.GetHashCode();
}

public record Bar(int A, int B) : Foo(A)
{
    public virtual bool Equals(Bar? other) => base.Equals(other); // CS8851 if you don't add the next line

    public override int GetHashCode() => base.GetHashCode(); // RCS1132, but removing the method implies a behavior change
}

Actual Behavior:
RCS1132

Expected Behavior:
No action triggered.

@josefpihrt
Copy link
Collaborator

But why would you (in a real world) intentionally call GetHashCode from a base class?

@eduherminio
Copy link
Member Author

I don't know how much sense does it make, but I'm playing with these kind of records (see dotnet/csharplang#4122 (comment) for more details):

public record Node(string Id)
{
    public virtual bool Equals(Node? other)
    {
        if (other is null)
        {
            return false;
        }

        return Id.Equals(other.Id);
    }

    public override int GetHashCode() => Id.GetHashCode();
}

public record TreeNode(string Id, ICollection<TreeNode> Children) : Node(Id)
{
    public override bool Equals(TreeNode? other) => base.Equals(other);

    public override int GetHashCode() => base.GetHashCode();
}

@nvmkpk
Copy link

nvmkpk commented Mar 30, 2021

If calling a custom implemented GetHashCode as in this example, it does not make sense but if we are calling the compiler generated GetHashCode it is ok to use it because hash code of EqualityContract property is also combined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants