Skip to content

Commit

Permalink
Merge pull request #659 from danielcweber/AvoidMoreObjectEquals
Browse files Browse the repository at this point in the history
Don't call object.Equals() from '=='-operators but call Equals on the underlying type.
  • Loading branch information
SteveDunn authored Aug 24, 2024
2 parents 986f494 + c8b3479 commit c50da2d
Show file tree
Hide file tree
Showing 30,561 changed files with 122,626 additions and 122,610 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
28 changes: 22 additions & 6 deletions src/Vogen/GenerateEqualsMethodsAndOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,30 @@ private static string GenerateEqualsMethodForPrimitive(VoWorkItem item, bool isR
public static string GenerateEqualsOperatorsForPrimitivesIfNeeded(string itemUnderlyingType, SyntaxToken typeName, VoWorkItem item)
{
if (item.Config.PrimitiveEqualityGeneration.HasFlag(PrimitiveEqualityGeneration.GenerateOperators))
return $"""
{
if (item.IsTheWrapperAReferenceType)
{
return $"""
public static global::System.Boolean operator ==({typeName} left, {itemUnderlyingType} right) => left?.Value.Equals(right) ?? false;
public static global::System.Boolean operator !=({typeName} left, {itemUnderlyingType} right) => !(left == right);
public static global::System.Boolean operator ==({itemUnderlyingType} left, {typeName} right) => right?.Value.Equals(left) ?? false;
public static global::System.Boolean operator !=({itemUnderlyingType} left, {typeName} right) => !(left == right);
""";
}
else
{
return $"""
public static global::System.Boolean operator ==({typeName} left, {itemUnderlyingType} right) => Equals(left.Value, right);
public static global::System.Boolean operator !=({typeName} left, {itemUnderlyingType} right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==({typeName} left, {itemUnderlyingType} right) => left.Value.Equals(right);
public static global::System.Boolean operator !=({typeName} left, {itemUnderlyingType} right) => !(left == right);
public static global::System.Boolean operator ==({itemUnderlyingType} left, {typeName} right) => Equals(left, right.Value);
public static global::System.Boolean operator !=({itemUnderlyingType} left, {typeName} right) => !Equals(left, right.Value);
""";
public static global::System.Boolean operator ==({itemUnderlyingType} left, {typeName} right) => right.Value.Equals(left);
public static global::System.Boolean operator !=({itemUnderlyingType} left, {typeName} right) => !(left == right);
""";
}
}

return string.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Age left, Age right) => left.Equals(right);
public static global::System.Boolean operator !=(Age left, Age right) => !(left == right);
public static global::System.Boolean operator ==(Age left, System.Int32 right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Age left, System.Int32 right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Int32 left, Age right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Int32 left, Age right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Age left, System.Int32 right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Age left, System.Int32 right) => !(left == right);
public static global::System.Boolean operator ==(System.Int32 left, Age right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Int32 left, Age right) => !(left == right);
public int CompareTo(Age other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down Expand Up @@ -840,10 +840,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Name left, Name right) => left.Equals(right);
public static global::System.Boolean operator !=(Name left, Name right) => !(left == right);
public static global::System.Boolean operator ==(Name left, System.String right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Name left, System.String right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.String left, Name right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.String left, Name right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Name left, System.String right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Name left, System.String right) => !(left == right);
public static global::System.Boolean operator ==(System.String left, Name right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.String left, Name right) => !(left == right);
public int CompareTo(Name other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !(left == right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Guid right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Guid right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Guid left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Guid left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Guid right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Guid right) => !(left == right);
public static global::System.Boolean operator ==(System.Guid left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Guid left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !(left == right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !(left == right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Guid right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Guid right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Guid left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Guid left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Guid right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Guid right) => !(left == right);
public static global::System.Boolean operator ==(System.Guid left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Guid left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !(left == right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !(left == right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !(left == right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Guid right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Guid right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Guid left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Guid left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Guid right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Guid right) => !(left == right);
public static global::System.Boolean operator ==(System.Guid left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Guid left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !(left == right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ private readonly global::System.Diagnostics.StackTrace _stackTrace = null;

public static global::System.Boolean operator ==(Vo left, Vo right) => left.Equals(right);
public static global::System.Boolean operator !=(Vo left, Vo right) => !(left == right);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => Equals(left.Value, right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !Equals(left.Value, right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => Equals(left, right.Value);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !Equals(left, right.Value);
public static global::System.Boolean operator ==(Vo left, System.Int32 right) => left.Value.Equals(right);
public static global::System.Boolean operator !=(Vo left, System.Int32 right) => !(left == right);
public static global::System.Boolean operator ==(System.Int32 left, Vo right) => right.Value.Equals(left);
public static global::System.Boolean operator !=(System.Int32 left, Vo right) => !(left == right);
public int CompareTo(Vo other) => Value.CompareTo(other.Value);
public int CompareTo(object other)
{
Expand Down
Loading

0 comments on commit c50da2d

Please sign in to comment.