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 6750f02
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 58 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 @@ -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 @@ -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
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
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ public virtual void EmitWriteLine(LocalBuilder localBuilder)
// one of the types for which Console.WriteLine implements overloads. (e.g.
// we do *not* call ToString on the locals.

Type consoleType = Type.GetType(ConsoleTypeFullName, throwOnError: true)!;
MethodInfo prop = consoleType.GetMethod("get_Out")!;
Emit(OpCodes.Call, prop);
Emit(OpCodes.Call, Type.GetType(ConsoleTypeFullName, throwOnError: true)!.GetMethod("get_Out")!);
Emit(OpCodes.Ldloc, localBuilder);
Type[] parameterTypes = new Type[1];

Type cls = localBuilder.LocalType;
if (cls is TypeBuilder || cls is EnumBuilder)
if (cls is TypeBuilder or EnumBuilder)
{
throw new ArgumentException(SR.NotSupported_OutputStreamUsingTypeBuilder);
}
parameterTypes[0] = cls;
MethodInfo mi = typeof(IO.TextWriter).GetMethod("WriteLine", parameterTypes) ?? throw new ArgumentException(SR.Argument_EmitWriteLineType, nameof(localBuilder));

MethodInfo mi = typeof(IO.TextWriter).GetMethod("WriteLine", [cls]) ??
throw new ArgumentException(SR.Argument_EmitWriteLineType, nameof(localBuilder));

Emit(OpCodes.Callvirt, mi);
}

Expand All @@ -142,9 +142,7 @@ public virtual void EmitWriteLine(FieldInfo fld)
// an error to call EmitWriteLine with a fld which is not of
// one of the types for which Console.WriteLine implements overloads. (e.g.
// we do *not* call ToString on the fields.
Type consoleType = Type.GetType(ConsoleTypeFullName, throwOnError: true)!;
MethodInfo prop = consoleType.GetMethod("get_Out")!;
Emit(OpCodes.Call, prop);
Emit(OpCodes.Call, Type.GetType(ConsoleTypeFullName, throwOnError: true)!.GetMethod("get_Out")!);

if ((fld.Attributes & FieldAttributes.Static) != 0)
{
Expand All @@ -155,14 +153,16 @@ public virtual void EmitWriteLine(FieldInfo fld)
Emit(OpCodes.Ldarg_0); // Load the this ref.
Emit(OpCodes.Ldfld, fld);
}
Type[] parameterTypes = new Type[1];

Type cls = fld.FieldType;
if (cls is TypeBuilder || cls is EnumBuilder)
if (cls is TypeBuilder or EnumBuilder)
{
throw new NotSupportedException(SR.NotSupported_OutputStreamUsingTypeBuilder);
}
parameterTypes[0] = cls;
MethodInfo mi = typeof(IO.TextWriter).GetMethod("WriteLine", parameterTypes) ?? throw new ArgumentException(SR.Argument_EmitWriteLineType, nameof(fld));

MethodInfo mi = typeof(IO.TextWriter).GetMethod("WriteLine", [cls]) ??
throw new ArgumentException(SR.Argument_EmitWriteLineType, nameof(fld));

Emit(OpCodes.Callvirt, mi);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ internal RuntimeResourceSet(IResourceReader reader) :
throw new ArgumentNullException(nameof(reader));
}

_defaultReader = reader as DeserializingResourceReader ?? throw new ArgumentException(SR.Format(SR.NotSupported_WrongResourceReader_Type, reader.GetType()), nameof(reader));
_defaultReader = reader as DeserializingResourceReader ??
throw new ArgumentException(SR.Format(SR.NotSupported_WrongResourceReader_Type, reader.GetType()), nameof(reader));

_resCache = new Dictionary<string, ResourceLocator>(FastResourceComparer.Default);

// in the CoreLib version RuntimeResourceSet creates ResourceReader and passes this in,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,9 @@ public static ContextualReflectionScope EnterContextualReflection(Assembly? acti
if (activating == null)
return new ContextualReflectionScope(null);

AssemblyLoadContext assemblyLoadContext = GetLoadContext(activating) ?? throw new ArgumentException(SR.Arg_MustBeRuntimeAssembly, nameof(activating));
AssemblyLoadContext assemblyLoadContext = GetLoadContext(activating) ??
throw new ArgumentException(SR.Arg_MustBeRuntimeAssembly, nameof(activating));

return assemblyLoadContext.EnterContextualReflection();
}

Expand Down

0 comments on commit 6750f02

Please sign in to comment.