Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Equals() and GetHashCode() implementations are inconsistent #362

@dougbu

Description

@dougbu

SourceLocation.GetHashCode() and SourceLocation.Equals() use different properties, a fairly basic no-no. problems are unlikely unless one of the source locations is corrupt. nonetheless this quirk should be removed.

in particular GetHashCode() ignores LineIndex and CharacterIndex while Equals() includes them in its comparison. Equals() is the odd one out because CompareTo() also ignores LineIndex and CharacterIndex.

e.g. the following test succeeds

// Arrange
var sourceLocationA = new SourceLocation(10, 3, 4);
var sourceLocationB = new SourceLocation(10, 45, 8754);

// Act
var compare = sourceLocationA.CompareTo(sourceLocationB);
var equals = sourceLocationA.Equals(sourceLocationB);
var hashCodeA = sourceLocationA.GetHashCode();
var hashCodeB = sourceLocationB.GetHashCode();

// Assert
Assert.Equal(0, compare);
Assert.False(equals);    // !!!
Assert.Equal(hashCodeA, hashCodeB);

original discussion at #357 (comment)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions