Nullable System.ComponentModel, System.Diagnostics.CodeAnalysis, System.Diagnostics...
authorAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Fri, 12 Apr 2019 14:29:33 +0000 (07:29 -0700)
committerStephen Toub <stoub@microsoft.com>
Fri, 12 Apr 2019 14:29:33 +0000 (10:29 -0400)
* Nullable System.ComponentModel, System.Diagnostics.CodeAnalysis, System.Diagnostics.Contracts

* Address PR feedback

src/System.Private.CoreLib/shared/System/ComponentModel/DefaultValueAttribute.cs
src/System.Private.CoreLib/shared/System/ComponentModel/EditorBrowsableAttribute.cs
src/System.Private.CoreLib/shared/System/ComponentModel/EditorBrowsableState.cs
src/System.Private.CoreLib/shared/System/Diagnostics/CodeAnalysis/SuppressMessageAttribute.cs
src/System.Private.CoreLib/shared/System/Diagnostics/Contracts/ContractFailedEventArgs.cs

index fb2b49c..9380064 100644 (file)
@@ -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
         /// <summary>
         /// This is the default value.
         /// </summary>
-        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;
 
         /// <summary>
         /// Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/>
         /// class, converting the specified value to the specified type, and using the U.S. English
         /// culture as the translation context.
         /// </summary>
-        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<Type, string, object>)));
                     }
 
-                    if (!(s_convertFromInvariantString is Func<Type, string, object> convertFromInvariantString))
+                    if (!(s_convertFromInvariantString is Func<Type?, string?, object> convertFromInvariantString))
                         return false;
 
                     try
@@ -162,7 +169,7 @@ namespace System.ComponentModel
         /// Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/>
         /// class using a <see cref='System.String'/>.
         /// </summary>
-        public DefaultValueAttribute(string value)
+        public DefaultValueAttribute(string? value)
         {
             _value = value;
         }
@@ -171,7 +178,7 @@ namespace System.ComponentModel
         /// Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/>
         /// class.
         /// </summary>
-        public DefaultValueAttribute(object value)
+        public DefaultValueAttribute(object? value)
         {
             _value = value;
         }
@@ -219,9 +226,9 @@ namespace System.ComponentModel
         /// <summary>
         /// Gets the default value of the property this attribute is bound to.
         /// </summary>
-        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;
     }
 }
index de69538..5ab2693 100644 (file)
@@ -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)
             {
index a98669c..bb935e8 100644 (file)
@@ -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
index 893d7b8..ae15b8e 100644 (file)
@@ -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; }
     }
 }
index 5e45bc5..ad19e84 100644 (file)
@@ -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