Skip to content

Simplify conditional operator #112074

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

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -177,7 +177,7 @@ internal void InitializeSourceInfo(bool fNeedFileInfo, Exception? exception)

public bool IsLastFrameFromForeignExceptionStackTrace(int i)
{
return (rgiLastFrameFromForeignExceptionStackTrace == null) ? false : rgiLastFrameFromForeignExceptionStackTrace[i];
return rgiLastFrameFromForeignExceptionStackTrace != null && rgiLastFrameFromForeignExceptionStackTrace[i];
}

public int GetNumberOfFrames() { return iFrameCount; }
Original file line number Diff line number Diff line change
@@ -798,7 +798,7 @@ protected virtual bool KeyEquals(object? item, object key)

if (_keycomparer != null)
return _keycomparer.Equals(item, key);
return item == null ? false : item.Equals(key);
return item != null && item.Equals(key);
}

// Returns a collection representing the keys of this hashtable. The order
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Convert.cs
Original file line number Diff line number Diff line change
@@ -314,12 +314,12 @@ internal static object DefaultToType(IConvertible value, Type targetType, IForma
// Conversions to Boolean
public static bool ToBoolean([NotNullWhen(true)] object? value)
{
return value == null ? false : ((IConvertible)value).ToBoolean(null);
return value != null && ((IConvertible)value).ToBoolean(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could be value is IConvertible convertible && convertible.ToBoolean(null), yes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a change in behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly a change in code generation, arguably for the worse: https://csharp.godbolt.org/z/nrM7df3YP

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What makes you say it's "worse"?

The code is nearly identical, just without the early exit for null.

}

public static bool ToBoolean([NotNullWhen(true)] object? value, IFormatProvider? provider)
{
return value == null ? false : ((IConvertible)value).ToBoolean(provider);
return value != null && ((IConvertible)value).ToBoolean(provider);
}

public static bool ToBoolean(bool value)
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Delegate.cs
Original file line number Diff line number Diff line change
@@ -193,7 +193,7 @@ public bool MoveNext()
return d1 is null;
}

return ReferenceEquals(d2, d1) ? true : d2.Equals((object?)d1);
return ReferenceEquals(d2, d1) || d2.Equals(d1);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -206,7 +206,7 @@ public bool MoveNext()
return d1 is not null;
}

return ReferenceEquals(d2, d1) ? false : !d2.Equals(d1);
return !ReferenceEquals(d2, d1) && !d2.Equals(d1);
}
}
}
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ protected MulticastDelegate([DynamicallyAccessedMembers(DynamicallyAccessedMembe
return d1 is null;
}

return ReferenceEquals(d2, d1) ? true : d2.Equals((object?)d1);
return ReferenceEquals(d2, d1) || d2.Equals(d1);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -50,7 +50,7 @@ protected MulticastDelegate([DynamicallyAccessedMembers(DynamicallyAccessedMembe
return d1 is not null;
}

return ReferenceEquals(d2, d1) ? false : !d2.Equals(d1);
return !ReferenceEquals(d2, d1) && !d2.Equals(d1);
}
}
#pragma warning restore CS0660, CS0661
Original file line number Diff line number Diff line change
@@ -196,7 +196,7 @@ public override string ToString()
return true;
}

return (left is null) ? false : left.Equals(right);
return left is not null && left.Equals(right);
}

public static bool operator !=(Assembly? left, Assembly? right) => !(left == right);
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ protected ConstructorInfo() { }
return true;
}

return (left is null) ? false : left.Equals(right);
return left is not null && left.Equals(right);
}

public static bool operator !=(ConstructorInfo? left, ConstructorInfo? right) => !(left == right);
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ public virtual void RemoveEventHandler(object? target, Delegate? handler)
return true;
}

return (left is null) ? false : left.Equals(right);
return left is not null && left.Equals(right);
}

public static bool operator !=(EventInfo? left, EventInfo? right) => !(left == right);
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ protected FieldInfo() { }
return true;
}

return (left is null) ? false : left.Equals(right);
return left is not null && left.Equals(right);
}

public static bool operator !=(FieldInfo? left, FieldInfo? right) => !(left == right);
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ public virtual Module Module
return true;
}

return (left is null) ? false : left.Equals(right);
return left is not null && left.Equals(right);
}

public static bool operator !=(MemberInfo? left, MemberInfo? right) => !(left == right);
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ this is ConstructorInfo &&
return true;
}

return (left is null) ? false : left.Equals(right);
return left is not null && left.Equals(right);
}

public static bool operator !=(MethodBase? left, MethodBase? right) => !(left == right);
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ protected MethodInfo() { }
return true;
}

return (left is null) ? false : left.Equals(right);
return left is not null && left.Equals(right);
}

public static bool operator !=(MethodInfo? left, MethodInfo? right) => !(left == right);
Original file line number Diff line number Diff line change
@@ -161,7 +161,7 @@ public virtual Type[] FindTypes(TypeFilter? filter, object? filterCriteria)
return true;
}

return (left is null) ? false : left.Equals(right);
return left is not null && left.Equals(right);
}

public static bool operator !=(Module? left, Module? right) => !(left == right);
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ protected PropertyInfo() { }
return true;
}

return (left is null) ? false : left.Equals(right);
return left is not null && left.Equals(right);
}

public static bool operator !=(PropertyInfo? left, PropertyInfo? right) => !(left == right);
6 changes: 3 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Type.cs
Original file line number Diff line number Diff line change
@@ -599,7 +599,7 @@ protected virtual TypeCode GetTypeCodeImpl()

public virtual InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) => throw new NotSupportedException(SR.NotSupported_SubclassOverride);

public virtual bool IsInstanceOfType([NotNullWhen(true)] object? o) => o == null ? false : IsAssignableFrom(o.GetType());
public virtual bool IsInstanceOfType([NotNullWhen(true)] object? o) => o != null && IsAssignableFrom(o.GetType());
public virtual bool IsEquivalentTo([NotNullWhen(true)] Type? other) => this == other;

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2085:UnrecognizedReflectionPattern",
@@ -689,15 +689,15 @@ internal string FormatTypeName()

public override string ToString() => "Type: " + Name; // Why do we add the "Type: " prefix?

public override bool Equals(object? o) => o == null ? false : Equals(o as Type);
public override bool Equals(object? o) => o != null && Equals(o as Type);
public override int GetHashCode()
{
Type systemType = UnderlyingSystemType;
if (!ReferenceEquals(systemType, this))
return systemType.GetHashCode();
return base.GetHashCode();
}
public virtual bool Equals(Type? o) => o == null ? false : ReferenceEquals(this.UnderlyingSystemType, o.UnderlyingSystemType);
public virtual bool Equals(Type? o) => o != null && ReferenceEquals(this.UnderlyingSystemType, o.UnderlyingSystemType);

[Intrinsic]
public static bool operator ==(Type? left, Type? right)
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Version.cs
Original file line number Diff line number Diff line change
@@ -451,7 +451,7 @@ private static bool TryParseComponent<TChar>(ReadOnlySpan<TChar> component, stri
}

// Quick reference equality test prior to calling the virtual Equality
return ReferenceEquals(v2, v1) ? true : v2.Equals(v1);
return ReferenceEquals(v2, v1) || v2.Equals(v1);
}

public static bool operator !=(Version? v1, Version? v2) => !(v1 == v2);
Loading
Oops, something went wrong.