From afb166b283a83667089db8550d42a050f6192f9e Mon Sep 17 00:00:00 2001 From: Anirudh Agnihotry Date: Fri, 12 Apr 2019 07:29:33 -0700 Subject: [PATCH] Nullable System.ComponentModel, System.Diagnostics.CodeAnalysis, System.Diagnostics.Contracts (dotnet/coreclr#23910) * Nullable System.ComponentModel, System.Diagnostics.CodeAnalysis, System.Diagnostics.Contracts * Address PR feedback Commit migrated from https://github.com/dotnet/coreclr/commit/3968451b40b5749c3072251ca571ab6b252bdb45 --- .../System/ComponentModel/DefaultValueAttribute.cs | 37 +++++++++++++--------- .../ComponentModel/EditorBrowsableAttribute.cs | 3 +- .../System/ComponentModel/EditorBrowsableState.cs | 1 + .../CodeAnalysis/SuppressMessageAttribute.cs | 15 +++++---- .../Contracts/ContractFailedEventArgs.cs | 17 +++++----- 5 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs index fb2b49c..9380064 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; @@ -20,31 +21,37 @@ namespace System.ComponentModel /// /// This is the default value. /// - private object _value; + private object? _value; // Delegate ad hoc created 'TypeDescriptor.ConvertFromInvariantString' reflection object cache - private static object s_convertFromInvariantString; + private static object? s_convertFromInvariantString; /// /// Initializes a new instance of the /// class, converting the specified value to the specified type, and using the U.S. English /// culture as the translation context. /// - public DefaultValueAttribute(Type type, string value) + public DefaultValueAttribute(Type? type, string? value) { - // The try/catch here is because attributes should never throw exceptions. + // The null check and try/catch here are because attributes should never throw exceptions. // We would fail to load an otherwise normal class. + + if (type == null) + { + return; + } + try { - if (TryConvertFromInvariantString(type, value, out object convertedValue)) + if (TryConvertFromInvariantString(type, value, out object? convertedValue)) { _value = convertedValue; } - else if (type.IsSubclassOf(typeof(Enum))) + else if (type.IsSubclassOf(typeof(Enum)) && value != null) { _value = Enum.Parse(type, value, true); } - else if (type == typeof(TimeSpan)) + else if (type == typeof(TimeSpan) && value != null) { _value = TimeSpan.Parse(value); } @@ -54,7 +61,7 @@ namespace System.ComponentModel } // Looking for ad hoc created TypeDescriptor.ConvertFromInvariantString(Type, string) - bool TryConvertFromInvariantString(Type typeToConvert, string stringValue, out object conversionResult) + bool TryConvertFromInvariantString(Type? typeToConvert, string? stringValue, out object? conversionResult) { conversionResult = null; @@ -62,11 +69,11 @@ namespace System.ComponentModel if (s_convertFromInvariantString == null) { Type typeDescriptorType = Type.GetType("System.ComponentModel.TypeDescriptor, System.ComponentModel.TypeConverter", throwOnError: false); - MethodInfo mi = typeDescriptorType?.GetMethod("ConvertFromInvariantString", BindingFlags.NonPublic | BindingFlags.Static); + MethodInfo? mi = typeDescriptorType?.GetMethod("ConvertFromInvariantString", BindingFlags.NonPublic | BindingFlags.Static); Volatile.Write(ref s_convertFromInvariantString, mi == null ? new object() : mi.CreateDelegate(typeof(Func))); } - if (!(s_convertFromInvariantString is Func convertFromInvariantString)) + if (!(s_convertFromInvariantString is Func convertFromInvariantString)) return false; try @@ -162,7 +169,7 @@ namespace System.ComponentModel /// Initializes a new instance of the /// class using a . /// - public DefaultValueAttribute(string value) + public DefaultValueAttribute(string? value) { _value = value; } @@ -171,7 +178,7 @@ namespace System.ComponentModel /// Initializes a new instance of the /// class. /// - public DefaultValueAttribute(object value) + public DefaultValueAttribute(object? value) { _value = value; } @@ -219,9 +226,9 @@ namespace System.ComponentModel /// /// Gets the default value of the property this attribute is bound to. /// - public virtual object Value => _value; + public virtual object? Value => _value; - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (obj == this) { @@ -242,6 +249,6 @@ namespace System.ComponentModel public override int GetHashCode() => base.GetHashCode(); - protected void SetValue(object value) => _value = value; + protected void SetValue(object? value) => _value = value; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/ComponentModel/EditorBrowsableAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/ComponentModel/EditorBrowsableAttribute.cs index de69538..5ab2693 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ComponentModel/EditorBrowsableAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ComponentModel/EditorBrowsableAttribute.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable namespace System.ComponentModel { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Delegate | AttributeTargets.Interface)] @@ -18,7 +19,7 @@ namespace System.ComponentModel public EditorBrowsableState State { get; } - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (obj == this) { diff --git a/src/libraries/System.Private.CoreLib/src/System/ComponentModel/EditorBrowsableState.cs b/src/libraries/System.Private.CoreLib/src/System/ComponentModel/EditorBrowsableState.cs index a98669c..bb935e8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ComponentModel/EditorBrowsableState.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ComponentModel/EditorBrowsableState.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable namespace System.ComponentModel { public enum EditorBrowsableState diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/SuppressMessageAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/SuppressMessageAttribute.cs index 893d7b8..ae15b8e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/SuppressMessageAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/SuppressMessageAttribute.cs @@ -12,6 +12,7 @@ ** ===========================================================*/ +#nullable enable namespace System.Diagnostics.CodeAnalysis { [AttributeUsage( @@ -23,17 +24,17 @@ namespace System.Diagnostics.CodeAnalysis [Conditional("CODE_ANALYSIS")] public sealed class SuppressMessageAttribute : Attribute { - public SuppressMessageAttribute(string category, string checkId) + public SuppressMessageAttribute(string? category, string? checkId) { Category = category; CheckId = checkId; } - public string Category { get; } - public string CheckId { get; } - public string Scope { get; set; } - public string Target { get; set; } - public string MessageId { get; set; } - public string Justification { get; set; } + public string? Category { get; } + public string? CheckId { get; } + public string? Scope { get; set; } + public string? Target { get; set; } + public string? MessageId { get; set; } + public string? Justification { get; set; } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Contracts/ContractFailedEventArgs.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Contracts/ContractFailedEventArgs.cs index 5e45bc5..ad19e84 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Contracts/ContractFailedEventArgs.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Contracts/ContractFailedEventArgs.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Collections.Generic; using System.Diagnostics; @@ -13,15 +14,15 @@ namespace System.Diagnostics.Contracts public sealed class ContractFailedEventArgs : EventArgs { private ContractFailureKind _failureKind; - private string _message; - private string _condition; - private Exception _originalException; + private string? _message; + private string? _condition; + private Exception? _originalException; private bool _handled; private bool _unwind; - internal Exception thrownDuringHandler; + internal Exception? thrownDuringHandler; - public ContractFailedEventArgs(ContractFailureKind failureKind, string message, string condition, Exception originalException) + public ContractFailedEventArgs(ContractFailureKind failureKind, string? message, string? condition, Exception? originalException) { Debug.Assert(originalException == null || failureKind == ContractFailureKind.PostconditionOnException); _failureKind = failureKind; @@ -30,10 +31,10 @@ namespace System.Diagnostics.Contracts _originalException = originalException; } - public string Message { get { return _message; } } - public string Condition { get { return _condition; } } + public string? Message { get { return _message; } } + public string? Condition { get { return _condition; } } public ContractFailureKind FailureKind { get { return _failureKind; } } - public Exception OriginalException { get { return _originalException; } } + public Exception? OriginalException { get { return _originalException; } } // Whether the event handler "handles" this contract failure, or to fail via escalation policy. public bool Handled -- 2.7.4