Skip to content

Commit

Permalink
Improve nullability for TypeConverter.CanConvertTo (#63874)
Browse files Browse the repository at this point in the history
The nullability of parameter 'destinationType' in
TypeConverter.CanConvertTo(...) was changed from
not-nullable to nullable during the development of .NET 6.

Since a destination type supported by this TypeConverter
can never be null, a NotNullWhenAttribute is added to the
'destinationType' parameter when the result value of
TypeConverter.CanConvertTo(...) is 'true'.

Fix #63186
  • Loading branch information
MichaelKetting committed Mar 1, 2022
1 parent 79026a5 commit ac246b1
Show file tree
Hide file tree
Showing 39 changed files with 82 additions and 66 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace System.ComponentModel
Expand Down Expand Up @@ -110,7 +111,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
return base.ConvertTo(context, culture, value, destinationType);
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
if (destinationType != null && destinationType.IsPrimitive)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
/// Gets a value indicating whether this converter can convert an object to
/// the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;

Expand All @@ -27,7 +28,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
/// Gets a value indicating whether this converter can convert an object
/// to the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace System.ComponentModel
Expand All @@ -26,7 +27,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
/// Gets a value indicating whether this converter can convert an
/// object to the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;

Expand All @@ -28,7 +29,7 @@ public class DecimalConverter : BaseNumberConverter
/// Gets a value indicating whether this converter can convert an
/// object to the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
/// Gets a value indicating whether this converter can convert an object to the
/// given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
if (destinationType == typeof(Enum[]) || destinationType == typeof(InstanceDescriptor))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;

Expand All @@ -27,7 +28,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
/// Gets a value indicating whether this converter can convert an object to
/// the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
/// <summary>
/// Gets a value indicating whether this converter can convert a value object to the destination type.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
if (destinationType == UnderlyingType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;

Expand All @@ -27,7 +28,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
/// Gets a value indicating whether this converter can convert an object to the given
/// destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public virtual bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceT
/// Gets a value indicating whether this converter can convert an object to the given
/// destination type using the context.
/// </summary>
public bool CanConvertTo(Type destinationType) => CanConvertTo(null, destinationType);
public bool CanConvertTo([NotNullWhen(true)] Type? destinationType) => CanConvertTo(null, destinationType);

/// <summary>
/// Gets a value indicating whether this converter can convert an object to the given
/// destination type using the context.
/// </summary>
public virtual bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public virtual bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(string);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace System.ComponentModel
Expand Down Expand Up @@ -37,7 +38,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
/// Gets a value indicating whether this converter can convert an object
/// to the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;

Expand All @@ -28,7 +29,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
/// Gets a value indicating whether this converter can
/// convert an object to the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(Uri) || destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;

Expand All @@ -27,7 +28,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
/// <para>Gets a value indicating whether this converter can
/// convert an object to the given destination type using the context.</para>
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(Version) || destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.Versioning;
Expand All @@ -12,7 +13,7 @@ namespace System.Security.Authentication.ExtendedProtection
{
public class ExtendedProtectionPolicyTypeConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Diagnostics.CodeAnalysis;

namespace System.ComponentModel.Tests
{
Expand Down Expand Up @@ -112,7 +113,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
public override bool CanConvertTo(ITypeDescriptorContext context, [NotNullWhen(true)] Type? destinationType)
{
if (destinationType == typeof(int))
{
Expand Down Expand Up @@ -189,7 +190,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
public override bool CanConvertTo(ITypeDescriptorContext context, [NotNullWhen(true)] Type? destinationType)
{
if (destinationType == typeof(int))
{
Expand Down Expand Up @@ -255,7 +256,7 @@ public ClassIDerived()

public class IBaseConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
public override bool CanConvertTo(ITypeDescriptorContext context, [NotNullWhen(true)] Type? destinationType)
{
if (destinationType == typeof(string) || destinationType == typeof(int))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ColumnTypeConverter() { }
/// <summary>
/// Gets a value indicating whether this converter can convert an object to the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) =>
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType) =>
destinationType == typeof(InstanceDescriptor) ||
base.CanConvertTo(context, destinationType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public DataColumnMappingConverter()
{
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
if (typeof(InstanceDescriptor) == destinationType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public DataTableMappingConverter()
{
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
if (typeof(InstanceDescriptor) == destinationType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace System.Data
Expand All @@ -16,7 +17,7 @@ public ConstraintConverter() { }
/// Gets a value indicating whether this converter can
/// convert an object to the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) =>
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType) =>
destinationType == typeof(InstanceDescriptor) ||
base.CanConvertTo(context, destinationType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace System.Data
Expand All @@ -15,7 +16,7 @@ public PrimaryKeyTypeConverter() : base(typeof(DataColumn[]))

public override bool GetPropertiesSupported(ITypeDescriptorContext? context) => false;

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) =>
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType) =>
destinationType == typeof(string) ||
base.CanConvertTo(context, destinationType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace System.Data
Expand All @@ -18,7 +19,7 @@ public RelationshipConverter()
/// Gets a value indicating whether this converter can
/// convert an object to the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
return base.ConvertFrom(context, culture, value);
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
// Only know how to convert to the NetworkLibrary enumeration
return ((typeof(string) == destinationType) || base.CanConvertTo(context, destinationType));
Expand Down Expand Up @@ -697,7 +697,7 @@ public OleDbConnectionStringBuilderConverter()
{
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
if (typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) == destinationType)
{
Expand Down
Loading

0 comments on commit ac246b1

Please sign in to comment.