From: Stephen Toub Date: Fri, 4 Oct 2019 20:53:22 +0000 (-0400) Subject: Remove unnecessary System.Linq usage from Microsoft.CSharp (dotnet/corefx#41552) X-Git-Tag: submit/tizen/20210909.063632~11031^2~362 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0d1c9d9a6eda8a2bec2dbec3e6158a93cd673a56;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Remove unnecessary System.Linq usage from Microsoft.CSharp (dotnet/corefx#41552) Most of the usage was just as easily (if not more easily) done without LINQ. In some cases the use was entirely superfluous (e.g. MethodReturningNewArray().ToArray()). Commit migrated from https://github.com/dotnet/corefx/commit/09698dd91c53e123d3a0f1240b5f611b3c14639c --- diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/RuntimeBinder.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/RuntimeBinder.cs index 8fff712..f177806 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/RuntimeBinder.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/RuntimeBinder.cs @@ -6,7 +6,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Dynamic; -using System.Linq; using System.Linq.Expressions; using Microsoft.CSharp.RuntimeBinder.Errors; using Microsoft.CSharp.RuntimeBinder.Semantics; @@ -1295,7 +1294,7 @@ namespace Microsoft.CSharp.RuntimeBinder LocalVariableSymbol[] locals) { Debug.Assert(arguments.Length >= 2); - Debug.Assert(arguments.All(a => a.Type != null)); + Debug.Assert(Array.TrueForAll(arguments, a => a.Type != null)); string name = payload.Name; diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/RuntimeBinderExtensions.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/RuntimeBinderExtensions.cs index d760efb..e08bc7d 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/RuntimeBinderExtensions.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/RuntimeBinderExtensions.cs @@ -4,7 +4,6 @@ using System; using System.Diagnostics; -using System.Linq; using System.Linq.Expressions; using System.Reflection; @@ -182,13 +181,25 @@ namespace Microsoft.CSharp.RuntimeBinder // Recurse in for generic types arrays, byref and pointer types. if (t1.IsGenericType && t2.IsGenericType) { - var args1 = t1.GetGenericArguments(); - var args2 = t2.GetGenericArguments(); + Type[] args1 = t1.GetGenericArguments(); + Type[] args2 = t2.GetGenericArguments(); if (args1.Length == args2.Length) { - return t1.IsGenericallyEqual(t2) && - Enumerable.All(Enumerable.Zip(args1, args2, (ta1, ta2) => ta1.IsGenericallyEquivalentTo(ta2, member1, member2)), x => x); + if (!t1.IsGenericallyEqual(t2)) + { + return false; + } + + for (int i = 0; i < args1.Length; i++) + { + if (!args1[i].IsGenericallyEquivalentTo(args2[i], member1, member2)) + { + return false; + } + } + + return true; } } @@ -330,11 +341,12 @@ namespace Microsoft.CSharp.RuntimeBinder string name = type.GetCustomAttribute()?.MemberName; if (name != null) { - if (type.GetProperties( - BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance) - .Any(p => p.Name == name && p.GetIndexParameters().Length != 0)) + foreach (PropertyInfo p in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)) { - return name; + if (p.Name == name && p.GetIndexParameters().Length != 0) + { + return name; + } } } diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Binding/Better.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Binding/Better.cs index 25e4a86..ca2cf71 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Binding/Better.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Binding/Better.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using Microsoft.CSharp.RuntimeBinder.Syntax; namespace Microsoft.CSharp.RuntimeBinder.Semantics @@ -484,9 +483,8 @@ LAgain: out CandidateFunctionMember methAmbig1, out CandidateFunctionMember methAmbig2) { - Debug.Assert(list.Any()); - Debug.Assert(list.First().mpwi != null); - Debug.Assert(list.Count > 0); + Debug.Assert(list.Count != 0); + Debug.Assert(list[0].mpwi != null); // select the best method: /* @@ -501,8 +499,8 @@ LAgain: goto phase 2 Phase 2: compare all items before candidate to candidate If candidate always better, return it, otherwise return null + */ - */ // Record two method that are ambiguous for error reporting. CandidateFunctionMember ambig1 = null; CandidateFunctionMember ambig2 = null; @@ -584,8 +582,8 @@ LAgain: { // For some reason, we have an ambiguity but never had a tie. // This can easily happen in a circular graph of candidate methods. - methAmbig1 = list.First(); - methAmbig2 = list.Skip(1).First(); + methAmbig1 = list[0]; + methAmbig2 = list[1]; } return null; diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/ExpressionBinder.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/ExpressionBinder.cs index 443912b..672bf8d 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/ExpressionBinder.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/ExpressionBinder.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using Microsoft.CSharp.RuntimeBinder.Errors; using Microsoft.CSharp.RuntimeBinder.Syntax; @@ -1106,7 +1105,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics int iDst = 0; int argCount = ExpressionIterator.Count(argsPtr); - Debug.Assert(!@params.Items.Any(p => p is ArgumentListType)); // We should never have picked a varargs method to bind to. + Debug.Assert(!Array.Exists(@params.Items, p => p is ArgumentListType)); // We should never have picked a varargs method to bind to. bool bDontFixParamArray = false; diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/MemberLookup.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/MemberLookup.cs index 98fc779..15993a1 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/MemberLookup.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/MemberLookup.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using Microsoft.CSharp.RuntimeBinder.Errors; using Microsoft.CSharp.RuntimeBinder.Syntax; @@ -317,7 +316,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics { return false; } - var o = field.AssociatedFieldInfo.GetCustomAttributes(typeof(System.Runtime.CompilerServices.DynamicAttribute), false).ToArray(); + var o = field.AssociatedFieldInfo.GetCustomAttributes(typeof(System.Runtime.CompilerServices.DynamicAttribute), false); if (o.Length == 1) { da = o[0] as System.Runtime.CompilerServices.DynamicAttribute; @@ -331,7 +330,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics { return false; } - var o = prop.AssociatedPropertyInfo.GetCustomAttributes(typeof(System.Runtime.CompilerServices.DynamicAttribute), false).ToArray(); + var o = prop.AssociatedPropertyInfo.GetCustomAttributes(typeof(System.Runtime.CompilerServices.DynamicAttribute), false); if (o.Length == 1) { da = o[0] as System.Runtime.CompilerServices.DynamicAttribute; diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeArray.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeArray.cs index bd36a6f..bcbbfa1 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeArray.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeArray.cs @@ -6,7 +6,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Linq; namespace Microsoft.CSharp.RuntimeBinder.Semantics { @@ -101,7 +100,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics public void AssertValid() { Debug.Assert(Count >= 0); - Debug.Assert(Items.All(item => item != null)); + Debug.Assert(Array.TrueForAll(Items, item => item != null)); } public void CopyItems(int i, int c, CType[] dest) => Array.Copy(Items, i, dest, 0, c); diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeManager.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeManager.cs index 8fb58bd..30b4808 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeManager.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeManager.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; @@ -750,19 +749,22 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics (Assembly, Assembly) key = (assemblyThatDefinesAttribute, assemblyToCheck); if (!s_internalsVisibleToCache.TryGetValue(key, out bool result)) { - AssemblyName assyName; - // Assembly.GetName() requires FileIOPermission to FileIOPermissionAccess.PathDiscovery. // If we don't have that (we're in low trust), then we are going to effectively turn off // InternalsVisibleTo. The alternative is to crash when this happens. try { - assyName = assemblyToCheck.GetName(); - result = assemblyThatDefinesAttribute.GetCustomAttributes() - .OfType() - .Select(ivta => new AssemblyName(ivta.AssemblyName)) - .Any(an => AssemblyName.ReferenceMatchesDefinition(an, assyName)); + AssemblyName assyName = assemblyToCheck.GetName(); + foreach (Attribute attr in assemblyThatDefinesAttribute.GetCustomAttributes()) + { + if (attr is InternalsVisibleToAttribute ivta && + AssemblyName.ReferenceMatchesDefinition(new AssemblyName(ivta.AssemblyName), assyName)) + { + result = true; + break; + } + } } catch (SecurityException) { diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Syntax/NameTable.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Syntax/NameTable.cs index 72c3d44..0d088f5 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Syntax/NameTable.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Syntax/NameTable.cs @@ -2,8 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Diagnostics; -using System.Linq; namespace Microsoft.CSharp.RuntimeBinder.Syntax { @@ -65,7 +65,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Syntax { int hashCode = ComputeHashCode(name.Text); // make sure it doesn't already exist - Debug.Assert(_entries.All(e => e?.Name.Text != name.Text)); + Debug.Assert(Array.TrueForAll(_entries, e => e?.Name.Text != name.Text)); AddEntry(name, hashCode); }