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

Don't call object.Equals() from '=='-operators but call Equals on the underlying type. #659

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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
Loading