Skip to content

Commit

Permalink
Fix more edge cases in code fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jan 4, 2025
1 parent a68ed9f commit 23acb10
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,18 @@ void HandleSetAccessor(IPropertySymbol propertySymbol, PropertyFlags propertyFla
continue;
}

// Execute the deferred default value validation, if necessary
if (fieldFlags.DefaultValueOperation is not null)
// Execute the deferred default value validation, if necessary. If we have an operation
// here, it means we had some constructed property metadata. Otherwise, it was 'null'.
if (fieldFlags.DefaultValueOperation is null)
{
// Special case: the whole property metadata is 'null', and the metadata type is nullable, but
// the property type isn't. In this case, we need to ensure the explicit 'null' is preserved.
if (fieldFlags.IsExplicitConversionFromNonNullableToNullableMetdataType(pair.Key.Type))
{
fieldFlags.DefaultValue = TypedConstantInfo.Null.Instance;
}
}
else
{
bool isNullableValueType = pair.Key.Type.IsNullableValueType();

Expand All @@ -523,9 +533,7 @@ void HandleSetAccessor(IPropertySymbol propertySymbol, PropertyFlags propertyFla
{
fieldFlags.DefaultValue = TypedConstantInfo.Null.Instance;
}
else if (!SymbolEqualityComparer.Default.Equals(pair.Key.Type, fieldFlags.PropertyType) &&
!pair.Key.Type.IsNullableValueType() &&
fieldFlags.PropertyType!.IsDefaultValueNull())
else if (fieldFlags.IsExplicitConversionFromNonNullableToNullableMetdataType(pair.Key.Type))
{
// Special case: the property type is not nullable, but the property metadata type is explicitly declared as
// a nullable type, and the default value is set to 'null'. In this case, we need to preserve this value.
Expand Down Expand Up @@ -773,6 +781,19 @@ private sealed class FieldFlags
/// The location of the target field being initialized.
/// </summary>
public Location? FieldLocation;

/// <summary>
/// Checks whether the field has an explicit conversion to a nullable metadata type (where the property isn't).
/// </summary>
/// <param name="propertyType">The property type.</param>
/// <returns>Whether the field has an explicit conversion to a nullable metadata type</returns>
public bool IsExplicitConversionFromNonNullableToNullableMetdataType(ITypeSymbol propertyType)
{
return
!SymbolEqualityComparer.Default.Equals(propertyType, PropertyType) &&
!propertyType.IsDefaultValueNull() &&
PropertyType!.IsDefaultValueNull();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2448,7 +2448,7 @@ public abstract partial class KeyFrame<TValue, TKeyFrame> : DependencyObject
[DataRow("Visibility", "Visibility", "new PropertyMetadata(default(Visibility))", "")]
[DataRow("Visibility", "Visibility", "new PropertyMetadata(Visibility.Visible)", "")]
[DataRow("Visibility", "Visibility", "new PropertyMetadata(Visibility.Collapsed)", "(DefaultValue = Visibility.Collapsed)")]
//[DataRow("Visibility", "object", "null", "(PropertyType = typeof(object), DefaultValue = null)")]
[DataRow("Visibility", "object", "null", "(PropertyType = typeof(object), DefaultValue = null)")]
[DataRow("Visibility", "object", "new PropertyMetadata(null)", "(PropertyType = typeof(object), DefaultValue = null)")]
[DataRow("Visibility", "object", "new PropertyMetadata(default(Visibility))", "(PropertyType = typeof(object))")]
[DataRow("Visibility", "object", "new PropertyMetadata(Visibility.Visible)", "(PropertyType = typeof(object))")]
Expand All @@ -2469,7 +2469,7 @@ public abstract partial class KeyFrame<TValue, TKeyFrame> : DependencyObject
[DataRow("MyEnum", "MyEnum", "new PropertyMetadata(default(MyEnum))", "")]
[DataRow("MyEnum", "MyEnum", "new PropertyMetadata(MyEnum.A)", "")]
[DataRow("MyEnum", "MyEnum", "new PropertyMetadata(MyEnum.B)", "(DefaultValue = MyEnum.B)")]
//[DataRow("MyEnum", "object", "null", "(PropertyType = typeof(object), DefaultValue = null)")]
[DataRow("MyEnum", "object", "null", "(PropertyType = typeof(object), DefaultValue = null)")]
[DataRow("MyEnum", "object", "new PropertyMetadata(null)", "(PropertyType = typeof(object), DefaultValue = null)")]
[DataRow("MyEnum", "object", "new PropertyMetadata(default(MyEnum))", "(PropertyType = typeof(object))")]
[DataRow("MyEnum", "object", "new PropertyMetadata(MyEnum.A)", "(PropertyType = typeof(object))")]
Expand Down

0 comments on commit 23acb10

Please sign in to comment.