Remove unnecessary null check from ConstraintCollection (dotnet/corefx#40343)
authorStephen Toub <stoub@microsoft.com>
Fri, 16 Aug 2019 15:25:21 +0000 (11:25 -0400)
committerGitHub <noreply@github.com>
Fri, 16 Aug 2019 15:25:21 +0000 (11:25 -0400)
`_table` will never be null.  The `ConstraintCollection` is only constructed in one place, where it's passed `this` by `DataTable`.  It's used in many other places without a guard check, as well (and later in this method, unguarded).

Commit migrated from https://github.com/dotnet/corefx/commit/f4344b0d3b45fc7c8f41e5c909ddcb2752bea0fa

src/libraries/System.Data.Common/src/System/Data/ConstraintCollection.cs

index aa0c6e8..c946535 100644 (file)
@@ -27,6 +27,7 @@ namespace System.Data
         /// </summary>
         internal ConstraintCollection(DataTable table)
         {
+            Debug.Assert(table != null);
             _table = table;
         }
 
@@ -420,18 +421,15 @@ namespace System.Data
         /// </summary>
         public void Clear()
         {
-            if (_table != null)
-            {
-                _table.PrimaryKey = null;
+            _table.PrimaryKey = null;
 
-                for (int i = 0; i < _table.ParentRelations.Count; i++)
-                {
-                    _table.ParentRelations[i].SetChildKeyConstraint(null);
-                }
-                for (int i = 0; i < _table.ChildRelations.Count; i++)
-                {
-                    _table.ChildRelations[i].SetParentKeyConstraint(null);
-                }
+            for (int i = 0; i < _table.ParentRelations.Count; i++)
+            {
+                _table.ParentRelations[i].SetChildKeyConstraint(null);
+            }
+            for (int i = 0; i < _table.ChildRelations.Count; i++)
+            {
+                _table.ChildRelations[i].SetParentKeyConstraint(null);
             }
 
             if (_table.fInitInProgress && _delayLoadingConstraints != null)