Skip to content

Commit

Permalink
Wrap any ?? throw news that go beyond 120 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jul 25, 2024
1 parent cd92f58 commit e392754
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ 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) ?? 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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ protected Delegate([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Al
return invoke.Invoke(this, BindingFlags.Default, null, args, null);
}


public override bool Equals([NotNullWhen(true)] object? obj)
{
if (obj == null || !InternalEqualTypes(this, obj))
Expand All @@ -107,9 +106,11 @@ public override bool Equals([NotNullWhen(true)] object? obj)
{
if (d._methodPtrAux != IntPtr.Zero)
return false; // different delegate kind

// they are both closed over the first arg
if (_target != d._target)
return false;

// fall through method handle check
}
else
Expand All @@ -121,19 +122,20 @@ public override bool Equals([NotNullWhen(true)] object? obj)
/*
if (_methodPtr != d._methodPtr)
return false;
*/
*/

if (_methodPtrAux == d._methodPtrAux)
return true;

// fall through method handle check
}

// method ptrs don't match, go down long path
//
if (_methodBase == null || d._methodBase == null || _methodBase is not MethodInfo || d._methodBase is not MethodInfo)
return InternalEqualMethodHandles(this, d);
else

if (_methodBase is MethodInfo && d._methodBase is MethodInfo)
return _methodBase.Equals(d._methodBase);
else
return InternalEqualMethodHandles(this, d);
}

public override int GetHashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,24 @@ public override void Emit(OpCode opcode, MethodInfo meth)
DynamicMethod? dynMeth = meth as DynamicMethod;
if (dynMeth == null)
{
RuntimeMethodInfo rtMeth = meth as RuntimeMethodInfo ?? 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);
else
token = GetTokenFor(rtMeth);
token = declaringType != null && (declaringType.IsGenericType || declaringType.IsArray) ?
GetTokenFor(rtMeth, declaringType) :
GetTokenFor(rtMeth);
}
else
{
// rule out not allowed operations on DynamicMethods
if (opcode.Equals(OpCodes.Ldtoken) || opcode.Equals(OpCodes.Ldftn) || opcode.Equals(OpCodes.Ldvirtftn))
if (opcode.Equals(OpCodes.Ldtoken) ||
opcode.Equals(OpCodes.Ldftn) ||
opcode.Equals(OpCodes.Ldvirtftn))
{
throw new ArgumentException(SR.Argument_InvalidOpCodeOnDynamicMethod);
}

token = GetTokenFor(dynMeth);
}

Expand Down Expand Up @@ -106,15 +110,13 @@ public override void Emit(OpCode opcode, ConstructorInfo con)
{
ArgumentNullException.ThrowIfNull(con);

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

if (declaringType != null && (declaringType.IsGenericType || declaringType.IsArray))
// need to sort out the stack size story
token = GetTokenFor(rtConstructor, declaringType);
else
token = GetTokenFor(rtConstructor);
RuntimeType declaringType = rtConstructor.GetRuntimeType();
int token = declaringType != null && (declaringType.IsGenericType || declaringType.IsArray) ?
GetTokenFor(rtConstructor, declaringType) : // need to sort out the stack size story
GetTokenFor(rtConstructor);

EnsureCapacity(7);
InternalEmit(opcode);
Expand All @@ -139,12 +141,12 @@ public override void Emit(OpCode opcode, FieldInfo field)
{
ArgumentNullException.ThrowIfNull(field);

RuntimeFieldInfo runtimeField = field as RuntimeFieldInfo ?? throw new ArgumentException(SR.Argument_MustBeRuntimeFieldInfo, nameof(field));
int token;
if (field.DeclaringType == null)
token = GetTokenFor(runtimeField);
else
token = GetTokenFor(runtimeField, runtimeField.GetRuntimeType());
RuntimeFieldInfo runtimeField = field as RuntimeFieldInfo ??
throw new ArgumentException(SR.Argument_MustBeRuntimeFieldInfo, nameof(field));

int token = field.DeclaringType == null ?
GetTokenFor(runtimeField) :
GetTokenFor(runtimeField, runtimeField.GetRuntimeType());

EnsureCapacity(7);
InternalEmit(opcode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ public override int GetMethodMetadataToken(ConstructorInfo constructor)
{
// some user derived ConstructorInfo
// go through the slower code path, i.e. retrieve parameters and form signature helper.
ParameterInfo[] parameters = constructor.GetParameters() ?? throw new ArgumentException(SR.Argument_InvalidConstructorInfo);
ParameterInfo[] parameters = constructor.GetParameters() ??
throw new ArgumentException(SR.Argument_InvalidConstructorInfo);

Type[] parameterTypes = new Type[parameters.Length];
Type[][] requiredCustomModifiers = new Type[parameters.Length][];
Type[][] optionalCustomModifiers = new Type[parameters.Length][];
Expand Down Expand Up @@ -990,7 +992,9 @@ private int GetMethodTokenNoLock(MethodInfo method, bool getGenericTypeDefinitio
}
else
{
Type declaringType = method.DeclaringType ?? throw new InvalidOperationException(SR.InvalidOperation_CannotImportGlobalFromDifferentModule);
Type declaringType = method.DeclaringType ??
throw new InvalidOperationException(SR.InvalidOperation_CannotImportGlobalFromDifferentModule);

if (declaringType.IsArray)
{
// use reflection to build signature to work around the E_T_VAR problem in EEClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ private void AddOneArgTypeHelper(Type clsArgument, Type[]? requiredCustomModifie
{
for (int i = 0; i < optionalCustomModifiers.Length; i++)
{
Type t = optionalCustomModifiers[i] ?? throw new ArgumentNullException(nameof(optionalCustomModifiers));
Type t = optionalCustomModifiers[i] ??
throw new ArgumentNullException(nameof(optionalCustomModifiers));

if (t.HasElementType)
throw new ArgumentException(SR.Argument_ArraysInvalid, nameof(optionalCustomModifiers));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,14 @@ private Delegate CreateDelegateInternal(Type delegateType, object? firstArgument
{
ArgumentNullException.ThrowIfNull(delegateType);

RuntimeType rtType = delegateType as RuntimeType ?? throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(delegateType));
RuntimeType rtType = delegateType as RuntimeType ??
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(delegateType));

if (!rtType.IsDelegate())
throw new ArgumentException(SR.Arg_MustBeDelegate, nameof(delegateType));

Delegate d = Delegate.CreateDelegateInternal(rtType, this, firstArgument, bindingFlags) ?? throw new ArgumentException(SR.Arg_DlgtTargMeth);
return d;
return Delegate.CreateDelegateInternal(rtType, this, firstArgument, bindingFlags) ??
throw new ArgumentException(SR.Arg_DlgtTargMeth);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ internal sealed partial class RuntimeModule : Module
RuntimeTypeHandle[] typeHandleArgs = new RuntimeTypeHandle[size];
for (int i = 0; i < size; i++)
{
Type typeArg = genericArguments[i] ?? throw new ArgumentException(SR.Argument_InvalidGenericInstArray);
typeArg = typeArg.UnderlyingSystemType;
if (typeArg == null)
throw new ArgumentException(SR.Argument_InvalidGenericInstArray);
Type? typeArg = genericArguments[i]?.UnderlyingSystemType;

if (typeArg is not System.RuntimeType)
throw new ArgumentException(SR.Argument_InvalidGenericInstArray);

typeHandleArgs[i] = typeArg.TypeHandle;
}
return typeHandleArgs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ private static unsafe ref byte GetSpanDataFrom(
[RequiresUnreferencedCode("Trimmer can't guarantee existence of class constructor")]
public static void RunClassConstructor(RuntimeTypeHandle type)
{
RuntimeType rt = type.GetRuntimeType() ?? throw new ArgumentException(SR.InvalidOperation_HandleIsNotInitialized, nameof(type));
RuntimeType rt = type.GetRuntimeType() ??
throw new ArgumentException(SR.InvalidOperation_HandleIsNotInitialized, nameof(type));

RunClassConstructor(new QCallTypeHandle(ref rt));
}

Expand All @@ -184,7 +186,9 @@ public static void RunClassConstructor(RuntimeTypeHandle type)

public static void RunModuleConstructor(ModuleHandle module)
{
RuntimeModule rm = module.GetRuntimeModule() ?? throw new ArgumentException(SR.InvalidOperation_HandleIsNotInitialized, nameof(module));
RuntimeModule rm = module.GetRuntimeModule() ??
throw new ArgumentException(SR.InvalidOperation_HandleIsNotInitialized, nameof(module));

RunModuleConstructor(new QCallModule(ref rm));
}

Expand All @@ -198,7 +202,8 @@ public static void RunModuleConstructor(ModuleHandle module)

public static unsafe void PrepareMethod(RuntimeMethodHandle method, RuntimeTypeHandle[]? instantiation)
{
IRuntimeMethodInfo methodInfo = method.GetMethodInfo() ?? throw new ArgumentException(SR.InvalidOperation_HandleIsNotInitialized, nameof(method));
IRuntimeMethodInfo methodInfo = method.GetMethodInfo() ??
throw new ArgumentException(SR.InvalidOperation_HandleIsNotInitialized, nameof(method));

// defensive copy of user-provided array, per CopyRuntimeTypeHandles contract
instantiation = (RuntimeTypeHandle[]?)instantiation?.Clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public static IntPtr OffsetOf(Type t, string fieldName)
{
ArgumentNullException.ThrowIfNull(t);

FieldInfo f = t.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? throw new ArgumentException(SR.Format(SR.Argument_OffsetOfFieldNotFound, t.FullName), nameof(fieldName));
FieldInfo f = t.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ??
throw new ArgumentException(SR.Format(SR.Argument_OffsetOfFieldNotFound, t.FullName), nameof(fieldName));

if (f is not RtFieldInfo rtField)
{
throw new ArgumentException(SR.Argument_MustBeRuntimeFieldInfo, nameof(fieldName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2730,7 +2730,9 @@ public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(Dyn

ArgumentNullException.ThrowIfNull(interfaceType);

RuntimeType ifaceRtType = interfaceType as RuntimeType ?? throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(interfaceType));
RuntimeType ifaceRtType = interfaceType as RuntimeType ??
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(interfaceType));

RuntimeTypeHandle ifaceRtTypeHandle = ifaceRtType.TypeHandle;

TypeHandle.VerifyInterfaceIsImplemented(ifaceRtTypeHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Interop.Errors.ERROR_BAD_NET_NAME or
Interop.Errors.ERROR_INVALID_PARAMETER or
Interop.Errors.ERROR_NETWORK_UNREACHABLE or
Interop.Errors.ERROR_NETWORK_ACCESS_DENIED or
Interop.Errors.ERROR_INVALID_HANDLE or
Interop.Errors.ERROR_FILENAME_EXCED_RANGE;
Interop.Errors.ERROR_INVALID_HANDLE or // eg from \\.\CON
Interop.Errors.ERROR_FILENAME_EXCED_RANGE; // Path is too long
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,9 @@ private static IntPtr InternalGetFunctionPointer(AssemblyLoadContext alc,
if (delegateType == null)
{
// Match search semantics of the CreateDelegate() function below.
BindingFlags bindingFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
MethodInfo methodInfo = type.GetMethod(methodName, bindingFlags) ?? throw new MissingMethodException(typeName, methodName);
const BindingFlags bindingFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
MethodInfo methodInfo = type.GetMethod(methodName, bindingFlags) ??
throw new MissingMethodException(typeName, methodName);

// Verify the function is properly marked.
if (null == methodInfo.GetCustomAttribute<UnmanagedCallersOnlyAttribute>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ internal AggregateException(string message, List<ExceptionDispatchInfo> innerExc
protected AggregateException(SerializationInfo info, StreamingContext context) :
base(info, context)
{
_innerExceptions = info.GetValue("InnerExceptions", typeof(Exception[])) as Exception[] ??
throw new SerializationException(SR.AggregateException_DeserializationFailure); // Do not rename (binary serialization);
_innerExceptions = info.GetValue("InnerExceptions", typeof(Exception[])) as Exception[] ?? // Do not rename (binary serialization);
throw new SerializationException(SR.AggregateException_DeserializationFailure);
}

/// <summary>
Expand Down
7 changes: 5 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/AppDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ public int ExecuteAssembly(string assemblyFile, string?[]? args, byte[]? hashVal

private static int ExecuteAssembly(Assembly assembly, string?[]? args)
{
MethodInfo entry = assembly.EntryPoint ?? throw new MissingMethodException(SR.Arg_EntryPointNotFoundException);
MethodInfo entry = assembly.EntryPoint ??
throw new MissingMethodException(SR.Arg_EntryPointNotFoundException);

object? result = entry.Invoke(
obj: null,
invokeAttr: BindingFlags.DoNotWrapExceptions,
Expand Down Expand Up @@ -419,7 +421,8 @@ public void SetThreadPrincipal(IPrincipal principal)
if (s_getWindowsPrincipal == null)
{
Type type = Type.GetType("System.Security.Principal.WindowsPrincipal, System.Security.Principal.Windows", throwOnError: true)!;
MethodInfo mi = type.GetMethod("GetDefaultInstance", BindingFlags.NonPublic | BindingFlags.Static) ?? throw new PlatformNotSupportedException(SR.PlatformNotSupported_Principal);
MethodInfo mi = type.GetMethod("GetDefaultInstance", BindingFlags.NonPublic | BindingFlags.Static) ??
throw new PlatformNotSupportedException(SR.PlatformNotSupported_Principal);
s_getWindowsPrincipal = mi.CreateDelegate<Func<IPrincipal>>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ public CultureInfo(string name, bool useUserOverride)
ArgumentNullException.ThrowIfNull(name);

// Get our data providing record
CultureData cultureData = CultureData.GetCultureData(name, useUserOverride) ?? throw new CultureNotFoundException(nameof(name), name, GetCultureNotSupportedExceptionMessage());
_cultureData = cultureData;
_cultureData = CultureData.GetCultureData(name, useUserOverride) ??
throw new CultureNotFoundException(nameof(name), name, GetCultureNotSupportedExceptionMessage());
_name = _cultureData.CultureName;
_isInherited = GetType() != typeof(CultureInfo);
}
Expand Down
Loading

0 comments on commit e392754

Please sign in to comment.