Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some more automated C# modernization in corelib #105151

Merged
merged 13 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -842,11 +842,7 @@ public string RequestLicKey(Type type)

((IDisposable?)parameters[2])?.Dispose();

var licenseKey = (string?)parameters[3];
if (licenseKey == null)
{
throw new COMException(); // E_FAIL
}
var licenseKey = (string?)parameters[3] ?? throw new COMException(); // E_FAIL
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

return licenseKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ public static unsafe void LoadInMemoryAssemblyInContext(IntPtr moduleHandle, Int
[RequiresUnreferencedCode("C++/CLI is not trim-compatible", Url = "https://aka.ms/dotnet-illink/nativehost")]
private static void LoadInMemoryAssemblyInContextImpl(IntPtr moduleHandle, IntPtr assemblyPath, AssemblyLoadContext? alc = null)
{
string? assemblyPathString = Marshal.PtrToStringUni(assemblyPath);
if (assemblyPathString == null)
throw new ArgumentOutOfRangeException(nameof(assemblyPath));
string assemblyPathString = Marshal.PtrToStringUni(assemblyPath) ?? throw new ArgumentOutOfRangeException(nameof(assemblyPath));

// We don't cache the ALCs or resolvers here since each IJW assembly will call this method at most once
// (the load process rewrites the stubs that call here to call the actual methods they're supposed to)
Expand Down
22 changes: 11 additions & 11 deletions src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected Delegate([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Al

if (target.ContainsGenericParameters)
throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target));
if (!(target is RuntimeType rtTarget))
if (target is not RuntimeType rtTarget)
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(target));

// This API existed in v1/v1.1 and only expected to create open
Expand Down Expand Up @@ -130,7 +130,7 @@ public override bool Equals([NotNullWhen(true)] object? obj)

// method ptrs don't match, go down long path
//
if (_methodBase == null || d._methodBase == null || !(_methodBase is MethodInfo) || !(d._methodBase is MethodInfo))
if (_methodBase == null || d._methodBase == null || _methodBase is not MethodInfo || d._methodBase is not MethodInfo)
return InternalEqualMethodHandles(this, d);
else
return _methodBase.Equals(d._methodBase);
Expand All @@ -156,7 +156,7 @@ public override int GetHashCode()

protected virtual MethodInfo GetMethodImpl()
{
if ((_methodBase == null) || !(_methodBase is MethodInfo))
if (_methodBase is null or not MethodInfo)
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
IRuntimeMethodInfo method = FindMethodHandle();
RuntimeType? declaringType = RuntimeMethodHandle.GetDeclaringType(method);
Expand Down Expand Up @@ -219,7 +219,7 @@ protected virtual MethodInfo GetMethodImpl()
ArgumentNullException.ThrowIfNull(target);
ArgumentNullException.ThrowIfNull(method);

if (!(type is RuntimeType rtType))
if (type is not RuntimeType rtType)
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
if (!rtType.IsDelegate())
throw new ArgumentException(SR.Arg_MustBeDelegate, nameof(type));
Expand Down Expand Up @@ -256,9 +256,9 @@ protected virtual MethodInfo GetMethodImpl()

if (target.ContainsGenericParameters)
throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target));
if (!(type is RuntimeType rtType))
if (type is not RuntimeType rtType)
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
if (!(target is RuntimeType rtTarget))
if (target is not RuntimeType rtTarget)
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(target));

if (!rtType.IsDelegate())
Expand Down Expand Up @@ -289,10 +289,10 @@ protected virtual MethodInfo GetMethodImpl()
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(method);

if (!(type is RuntimeType rtType))
if (type is not RuntimeType rtType)
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));

if (!(method is RuntimeMethodInfo rmi))
if (method is not RuntimeMethodInfo rmi)
throw new ArgumentException(SR.Argument_MustBeRuntimeMethodInfo, nameof(method));

if (!rtType.IsDelegate())
Expand Down Expand Up @@ -324,10 +324,10 @@ protected virtual MethodInfo GetMethodImpl()
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(method);

if (!(type is RuntimeType rtType))
if (type is not RuntimeType rtType)
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));

if (!(method is RuntimeMethodInfo rmi))
if (method is not RuntimeMethodInfo rmi)
throw new ArgumentException(SR.Argument_MustBeRuntimeMethodInfo, nameof(method));

if (!rtType.IsDelegate())
Expand Down Expand Up @@ -362,7 +362,7 @@ internal static Delegate CreateDelegateNoSecurityCheck(Type type, object? target
if (method.IsNullHandle())
throw new ArgumentNullException(nameof(method));

if (!(type is RuntimeType rtType))
if (type is not RuntimeType rtType)
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));

if (!rtType.IsDelegate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ protected sealed override Delegate CombineImpl(Delegate? follow)
followCount = (int)dFollow._invocationCount;

int resultCount;
if (!(_invocationList is object[] invocationList))
if (_invocationList is not object[] invocationList)
{
resultCount = 1 + followCount;
resultList = new object[resultCount];
Expand Down Expand Up @@ -335,9 +335,9 @@ private static bool EqualInvocationLists(object[] a, object[] b, int start, int

if (v == null)
return this;
if (!(v._invocationList is object[]))
if (v._invocationList is not object[])
{
if (!(_invocationList is object[] invocationList))
if (_invocationList is not object[] invocationList)
{
// they are both not real Multicast
if (this.Equals(value))
Expand Down Expand Up @@ -401,7 +401,7 @@ private static bool EqualInvocationLists(object[] a, object[] b, int start, int
public sealed override Delegate[] GetInvocationList()
{
Delegate[] del;
if (!(_invocationList is object[] invocationList))
if (_invocationList is not object[] invocationList)
{
del = new Delegate[1];
del[0] = this;
Expand All @@ -418,12 +418,12 @@ public sealed override Delegate[] GetInvocationList()
return del;
}

internal new bool HasSingleTarget => !(_invocationList is object[]);
internal new bool HasSingleTarget => _invocationList is not object[];

// Used by delegate invocation list enumerator
internal object? /* Delegate? */ TryGetAt(int index)
{
if (!(_invocationList is object[] invocationList))
if (_invocationList is not object[] invocationList)
{
return (index == 0) ? this : null;
}
Expand All @@ -447,7 +447,7 @@ public sealed override int GetHashCode()
}
}

if (!(_invocationList is object[] invocationList))
if (_invocationList is not object[] invocationList)
{
return base.GetHashCode();
}
Expand Down Expand Up @@ -515,7 +515,7 @@ protected override MethodInfo GetMethodImpl()
{
// we handle unmanaged function pointers here because the generic ones (used for WinRT) would otherwise
// be treated as open delegates by the base implementation, resulting in failure to get the MethodInfo
if ((_methodBase == null) || !(_methodBase is MethodInfo))
if (_methodBase is null or not MethodInfo)
{
IRuntimeMethodInfo method = FindMethodHandle();
RuntimeType declaringType = RuntimeMethodHandle.GetDeclaringType(method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr
for (i = 0; i < namedProperties.Length; i++)
{
// Validate the property.
PropertyInfo property = namedProperties[i];
if (property == null)
throw new ArgumentNullException("namedProperties[" + i + "]");
PropertyInfo property = namedProperties[i] ?? throw new ArgumentNullException($"namedProperties[{i}]");

// Allow null for non-primitive types only.
Type propType = property.PropertyType;
Expand All @@ -150,7 +148,7 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr

// Property has to be from the same class or base class as ConstructorInfo.
if (property.DeclaringType != con.DeclaringType
&& (!(con.DeclaringType is TypeBuilderInstantiation))
&& (con.DeclaringType is not TypeBuilderInstantiation)
&& !con.DeclaringType!.IsSubclassOf(property.DeclaringType!))
{
// Might have failed check because one type is a XXXBuilder
Expand All @@ -162,7 +160,7 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr
// the constructor is a TypeBuilder, but we still need
// to deal with the case where the property's declaring
// type is one.
if (!(property.DeclaringType is TypeBuilder) ||
if (property.DeclaringType is not TypeBuilder ||
!con.DeclaringType.IsSubclassOf(((RuntimeTypeBuilder)property.DeclaringType).BakedRuntimeType))
throw new ArgumentException(SR.Argument_BadPropertyForConstructorBuilder);
}
Expand All @@ -188,9 +186,7 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr
for (i = 0; i < namedFields.Length; i++)
{
// Validate the field.
FieldInfo namedField = namedFields[i];
if (namedField == null)
throw new ArgumentNullException("namedFields[" + i + "]");
FieldInfo namedField = namedFields[i] ?? throw new ArgumentNullException($"namedFields[{i}]");

// Allow null for non-primitive types only.
Type fldType = namedField.FieldType;
Expand All @@ -204,7 +200,7 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr

// Field has to be from the same class or base class as ConstructorInfo.
if (namedField.DeclaringType != con.DeclaringType
&& (!(con.DeclaringType is TypeBuilderInstantiation))
&& (con.DeclaringType is not TypeBuilderInstantiation)
&& !con.DeclaringType!.IsSubclassOf(namedField.DeclaringType!))
{
// Might have failed check because one type is a XXXBuilder
Expand All @@ -216,7 +212,7 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr
// the constructor is a TypeBuilder, but we still need
// to deal with the case where the field's declaring
// type is one.
if (!(namedField.DeclaringType is TypeBuilder) ||
if (namedField.DeclaringType is not TypeBuilder ||
!con.DeclaringType.IsSubclassOf(((RuntimeTypeBuilder)namedFields[i].DeclaringType!).BakedRuntimeType))
throw new ArgumentException(SR.Argument_BadFieldForConstructorBuilder);
}
Expand Down Expand Up @@ -255,20 +251,11 @@ private static bool ValidateType(Type t)
}
if (t.IsEnum)
{
switch (Type.GetTypeCode(Enum.GetUnderlyingType(t)))
{
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
return true;
default:
return false;
}
return Type.GetTypeCode(Enum.GetUnderlyingType(t)) is
TypeCode.SByte or TypeCode.Byte or
TypeCode.Int16 or TypeCode.UInt16 or
TypeCode.Int32 or TypeCode.UInt32 or
TypeCode.Int64 or TypeCode.UInt64;
}
if (t.IsArray)
{
Expand Down Expand Up @@ -429,8 +416,7 @@ private static void EmitValue(BinaryWriter writer, Type type, object? value)
writer.Write((byte)0xff);
else
{
string? typeName = TypeNameBuilder.ToString((Type)value, TypeNameBuilder.Format.AssemblyQualifiedName);
if (typeName == null)
string typeName = TypeNameBuilder.ToString((Type)value, TypeNameBuilder.Format.AssemblyQualifiedName) ??
throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeForCA, value.GetType()));
EmitString(writer, typeName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,13 @@ internal void GetCallableMethod(RuntimeModule module, DynamicMethod dm)
public override LocalBuilder DeclareLocal(Type localType, bool pinned)
{
ArgumentNullException.ThrowIfNull(localType);

RuntimeLocalBuilder localBuilder;

RuntimeType? rtType = localType as RuntimeType;

if (rtType == null)
if (localType is not RuntimeType)
{
throw new ArgumentException(SR.Argument_MustBeRuntimeType);
}

localBuilder = new RuntimeLocalBuilder(m_localCount, localType, m_methodBuilder, pinned);
// add the localType to local signature
RuntimeLocalBuilder localBuilder = new RuntimeLocalBuilder(m_localCount, localType, m_methodBuilder, pinned);
m_localSignature.AddArgument(localType, pinned);
m_localCount++;
return localBuilder;
Expand All @@ -64,10 +61,7 @@ public override void Emit(OpCode opcode, MethodInfo meth)
DynamicMethod? dynMeth = meth as DynamicMethod;
if (dynMeth == null)
{
RuntimeMethodInfo? rtMeth = meth as RuntimeMethodInfo;
if (rtMeth == null)
throw new ArgumentException(SR.Argument_MustBeRuntimeMethodInfo, nameof(meth));

RuntimeMethodInfo rtMeth = meth as RuntimeMethodInfo ?? throw new ArgumentException(SR.Argument_MustBeRuntimeMethodInfo, nameof(meth));
RuntimeType declaringType = rtMeth.GetRuntimeType();
if (declaringType != null && (declaringType.IsGenericType || declaringType.IsArray))
token = GetTokenFor(rtMeth, declaringType);
Expand Down Expand Up @@ -112,10 +106,7 @@ public override void Emit(OpCode opcode, ConstructorInfo con)
{
ArgumentNullException.ThrowIfNull(con);

RuntimeConstructorInfo? rtConstructor = con as RuntimeConstructorInfo;
if (rtConstructor == null)
throw new ArgumentException(SR.Argument_MustBeRuntimeMethodInfo, nameof(con));

RuntimeConstructorInfo rtConstructor = con as RuntimeConstructorInfo ?? throw new ArgumentException(SR.Argument_MustBeRuntimeMethodInfo, nameof(con));
RuntimeType declaringType = rtConstructor.GetRuntimeType();
int token;

Expand All @@ -137,11 +128,7 @@ public override void Emit(OpCode opcode, Type type)
{
ArgumentNullException.ThrowIfNull(type);

RuntimeType? rtType = type as RuntimeType;

if (rtType == null)
throw new ArgumentException(SR.Argument_MustBeRuntimeType);

RuntimeType rtType = type as RuntimeType ?? throw new ArgumentException(SR.Argument_MustBeRuntimeType);
int token = GetTokenFor(rtType);
EnsureCapacity(7);
InternalEmit(opcode);
Expand All @@ -152,10 +139,7 @@ public override void Emit(OpCode opcode, FieldInfo field)
{
ArgumentNullException.ThrowIfNull(field);

RuntimeFieldInfo? runtimeField = field as RuntimeFieldInfo;
if (runtimeField == null)
throw new ArgumentException(SR.Argument_MustBeRuntimeFieldInfo, nameof(field));

RuntimeFieldInfo runtimeField = field as RuntimeFieldInfo ?? throw new ArgumentException(SR.Argument_MustBeRuntimeFieldInfo, nameof(field));
int token;
if (field.DeclaringType == null)
token = GetTokenFor(runtimeField);
Expand Down Expand Up @@ -280,7 +264,7 @@ public override void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? opti
stackchange -= methodInfo.GetParameterTypes().Length;
// Pop the this parameter if the method is non-static and the
// instruction is not newobj.
if (!(methodInfo is SymbolMethod) && !methodInfo.IsStatic && !opcode.Equals(OpCodes.Newobj))
if (methodInfo is not SymbolMethod && !methodInfo.IsStatic && !opcode.Equals(OpCodes.Newobj))
stackchange--;
// Pop the optional parameters off the stack.
if (optionalParameterTypes != null)
Expand Down Expand Up @@ -783,11 +767,7 @@ internal override void ResolveToken(int token, out IntPtr typeHandle, out IntPtr
methodHandle = default;
fieldHandle = default;

object? handle = m_scope[token];

if (handle == null)
throw new InvalidProgramException();

object handle = m_scope[token] ?? throw new InvalidProgramException();
if (handle is RuntimeTypeHandle)
{
typeHandle = ((RuntimeTypeHandle)handle).Value;
Expand Down Expand Up @@ -1010,7 +990,7 @@ internal int GetTokenFor(VarArgMethod varArgMethod)
if (fromMethod == 0)
return (byte[]?)this[token];

if (!(this[token] is VarArgMethod vaMethod))
if (this[token] is not VarArgMethod vaMethod)
return null;

return vaMethod.m_signature.GetSignature(true);
Expand Down
Loading
Loading