Skip to content

Commit

Permalink
Fix more warnings from updated IDE rules (#71779)
Browse files Browse the repository at this point in the history
A newer version of Microsoft.CodeAnalysis.* causes some of the rules to fire in more cases.  Fixing them proactively before we ingest newer versions of these libs.
  • Loading branch information
stephentoub authored Jul 8, 2022
1 parent 5c33f2b commit 983c8f2
Show file tree
Hide file tree
Showing 360 changed files with 973 additions and 3,004 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,7 @@ public string RequestLicKey(Type type)
throw new COMException(); // E_FAIL
}

var license = (IDisposable?)parameters[2];
if (license != null)
{
license.Dispose();
}
((IDisposable?)parameters[2])?.Dispose();

var licenseKey = (string?)parameters[3];
if (licenseKey == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,11 +821,13 @@ internal RuntimeParameterInfo[] LoadParameters()
Type[] parameterTypes = m_owner.m_parameterTypes;
RuntimeParameterInfo[] parameters = new RuntimeParameterInfo[parameterTypes.Length];
for (int i = 0; i < parameterTypes.Length; i++)
{
parameters[i] = new RuntimeParameterInfo(this, null, parameterTypes[i], i);
if (m_parameters == null)
// should we interlockexchange?
m_parameters = parameters;
}

m_parameters ??= parameters; // should we Interlocked.CompareExchange?
}

return m_parameters;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,9 +1180,11 @@ internal static Exception GetCOMHRExceptionObject(int hr, IntPtr pCPCMD, object
{
Exception? ex = s_pendingExceptionObject;
if (ex != null)
{
ex.InternalPreserveStackTrace();
s_pendingExceptionObject = null;
}

s_pendingExceptionObject = null;
return ex;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ private IEnumerable<CustomAttributeData> GetMatchingCustomAttributesIterator(E e
}
else
{
if (usage == null)
usage = GetAttributeUsage(attributeType);
usage ??= GetAttributeUsage(attributeType);
encounteredTypes[attributeTypeKey] = usage;
// Type was encountered at a lower level. Only include it if its inheritable AND allowMultiple.
if (usage.Inherited && usage.AllowMultiple)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public static object CreateInstance(
type = type.UnderlyingSystemType;
CreateInstanceCheckType(type);

if (args == null)
args = Array.Empty<object>();
args ??= Array.Empty<object>();
int numArgs = args.Length;

Type?[] argTypes = new Type[numArgs];
Expand All @@ -85,8 +84,7 @@ public static object CreateInstance(
throw new MissingMethodException(SR.Format(SR.Arg_NoDefCTor, type));
}

if (binder == null)
binder = Type.DefaultBinder;
binder ??= Type.DefaultBinder;

MethodBase invokeMethod = binder.BindToMethod(bindingAttr, matches.ToArray(), ref args, null, culture, null, out object? state);
if (invokeMethod.GetParametersNoCopy().Length == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,7 @@ internal static object DynamicInvokeBoxedValuetypeReturn(out DynamicInvokeParamL
{
if (paramType == DynamicInvokeParamType.Ref)
{
if (nullableCopyBackObjects == null)
{
nullableCopyBackObjects = new object[parameters.Length];
}
nullableCopyBackObjects ??= new object[parameters.Length];

nullableCopyBackObjects[index] = finalObjectToReturn;
parameters[index] = null;
Expand Down Expand Up @@ -667,10 +664,7 @@ public static object DynamicInvokeParamHelperCore(ref ArgSetupState argSetupStat
{
// Since this not a by-ref parameter, we don't want to bash the original user-owned argument array but the rules of DynamicInvokeParamHelperCore() require
// that we return non-value types as the "index"th element in an array. Thus, create an on-demand throwaway array just for this purpose.
if (argSetupState.customBinderProvidedParameters == null)
{
argSetupState.customBinderProvidedParameters = new object[argSetupState.parameters.Length];
}
argSetupState.customBinderProvidedParameters ??= new object[argSetupState.parameters.Length];
argSetupState.customBinderProvidedParameters[index] = incomingParam;
return argSetupState.customBinderProvidedParameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ protected sealed override IEnumerable<TypeForwardInfo> TypeForwardInfos
string? namespaceName = null;
foreach (TypeForwarderHandle typeForwarderHandle in namespaceHandle.GetNamespaceDefinition(reader).TypeForwarders)
{
if (namespaceName == null)
{
namespaceName = namespaceHandle.ToNamespaceName(reader);
}
namespaceName ??= namespaceHandle.ToNamespaceName(reader);

TypeForwarder typeForwarder = typeForwarderHandle.GetTypeForwarder(reader);
string typeName = typeForwarder.Name.GetString(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ public sealed override Type[] GetForwardedTypes()
}
else
{
if (exceptions == null)
{
exceptions = new List<Exception>();
}
exceptions ??= new List<Exception>();
exceptions.Add(exception);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public RuntimePseudoCustomAttributeData(
Type attributeType, IList<CustomAttributeTypedArgument> constructorArguments)
{
_attributeType = attributeType;
if (constructorArguments == null)
constructorArguments = Array.Empty<CustomAttributeTypedArgument>();
constructorArguments ??= Array.Empty<CustomAttributeTypedArgument>();
_constructorArguments = new ReadOnlyCollection<CustomAttributeTypedArgument>(constructorArguments);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public sealed override ParameterInfo[] GetParametersNoCopy()
[DebuggerGuidedStepThrough]
public sealed override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture)
{
if (parameters == null)
parameters = Array.Empty<object>();
parameters ??= Array.Empty<object>();
MethodInvoker methodInvoker;
try
{
Expand Down Expand Up @@ -167,11 +166,7 @@ protected MethodInvoker MethodInvoker
{
get
{
if (_lazyMethodInvoker == null)
{
_lazyMethodInvoker = UncachedMethodInvoker;
}
return _lazyMethodInvoker;
return _lazyMethodInvoker ??= UncachedMethodInvoker;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ internal static string ComputeToString(MethodBase contextMethod, RuntimeTypeInfo
{
sb.Append(sep);
sep = ",";
string? name = methodTypeArgument.InternalNameIfAvailable;
if (name == null)
name = Type.DefaultTypeNameWhenMissingMetadata;
string name =
methodTypeArgument.InternalNameIfAvailable ??
Type.DefaultTypeNameWhenMissingMetadata;
sb.Append(methodTypeArgument.Name);
}
sb.Append(']');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ public sealed override ParameterInfo[] GetParametersNoCopy()
[DebuggerGuidedStepThroughAttribute]
public sealed override object? Invoke(object? obj, BindingFlags invokeAttr, Binder binder, object?[]? parameters, CultureInfo culture)
{
if (parameters == null)
parameters = Array.Empty<object>();
parameters ??= Array.Empty<object>();
MethodInvoker methodInvoker = this.MethodInvoker;
object? result = methodInvoker.Invoke(obj, parameters, binder, invokeAttr, culture);
System.Diagnostics.DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public sealed override Type DeclaringType
[DebuggerGuidedStepThrough]
public sealed override object Invoke(BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture)
{
if (parameters == null)
parameters = Array.Empty<object>();
parameters ??= Array.Empty<object>();

// Most objects are allocated by NewObject and their constructors return "void". But in many frameworks,
// there are "weird" cases (e.g. String) where the constructor must do both the allocation and initialization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public sealed override int MetadataToken
[DebuggerGuidedStepThrough]
public sealed override object Invoke(BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture)
{
if (parameters == null)
parameters = Array.Empty<object>();
parameters ??= Array.Empty<object>();

object ctorAllocatedObject = this.MethodInvoker.Invoke(null, parameters, binder, invokeAttr, culture)!;
System.Diagnostics.DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ public sealed override MethodInfo GetMethod

_lazyGetterInvoker = Getter.GetUncachedMethodInvoker(Array.Empty<RuntimeTypeInfo>(), this);
}
if (index == null)
index = Array.Empty<object>();
index ??= Array.Empty<object>();
return _lazyGetterInvoker.Invoke(obj, index, binder, invokeAttr, culture);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ private NamespaceChain NamespaceChain
{
get
{
if (_lazyNamespaceChain == null)
_lazyNamespaceChain = new NamespaceChain(_reader, _typeDefinition.NamespaceDefinition);
return _lazyNamespaceChain;
return _lazyNamespaceChain ??= new NamespaceChain(_reader, _typeDefinition.NamespaceDefinition);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ public sealed override string ToString()
}
}
// If all else fails, use the ToString() - it won't match the legacy CLR but with no metadata, we can't match it anyway.
if (genericTypeDefinitionString == null)
genericTypeDefinitionString = genericTypeDefinition.ToString();
genericTypeDefinitionString ??= genericTypeDefinition.ToString();

// Now, append the generic type arguments.
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ protected sealed override ConstructorInfo GetConstructorImpl(BindingFlags bindin
if ((bindingAttr & BindingFlags.ExactBinding) != 0)
return System.DefaultBinder.ExactBinding(candidates.ToArray(), types) as ConstructorInfo;

if (binder == null)
binder = DefaultBinder;
binder ??= DefaultBinder;

return binder.SelectMethod(bindingAttr, candidates.ToArray(), types, modifiers) as ConstructorInfo;
}
Expand Down Expand Up @@ -108,8 +107,7 @@ private MethodInfo GetMethodImplCommon(string name, int genericParameterCount, B
if (types.Length == 0 && candidates.Count == 1)
return candidates[0];

if (binder == null)
binder = DefaultBinder;
binder ??= DefaultBinder;

return binder.SelectMethod(bindingAttr, candidates.ToArray(), types, modifiers) as MethodInfo;
}
Expand Down Expand Up @@ -175,8 +173,7 @@ protected sealed override PropertyInfo GetPropertyImpl(string name, BindingFlags
if ((bindingAttr & BindingFlags.ExactBinding) != 0)
return System.DefaultBinder.ExactPropertyBinding(candidates.ToArray(), returnType, types);

if (binder == null)
binder = DefaultBinder;
binder ??= DefaultBinder;

return binder.SelectProperty(bindingAttr, candidates.ToArray(), returnType, types, modifiers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ internal abstract partial class RuntimeTypeInfo
int argCnt = (providedArgs != null) ? providedArgs.Length : 0;

#region Get a Binder
if (binder == null)
binder = DefaultBinder;
binder ??= DefaultBinder;

#endregion

Expand Down Expand Up @@ -375,11 +374,9 @@ internal abstract partial class RuntimeTypeInfo
return finalist.Invoke(target, bindingFlags, binder, providedArgs, culture);
}

if (finalists == null)
finalists = new MethodInfo[] { finalist };
finalists ??= new MethodInfo[] { finalist };

if (providedArgs == null)
providedArgs = Array.Empty<object>();
providedArgs ??= Array.Empty<object>();

object? state = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,7 @@ internal void EstablishDebugName()
if (_debugName == null)
{
_debugName = "Constructing..."; // Protect against any inadvertent reentrancy.
string debugName;
debugName = this.ToString();
if (debugName == null)
debugName = "";
_debugName = debugName;
_debugName = ToString() ?? "";
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,7 @@ public static int MarkThreadAsBlocked(int managedThreadId, CctorHandle blockedOn
#endif
using (LockHolder.Hold(s_cctorGlobalLock))
{
if (s_blockingRecords == null)
s_blockingRecords = new BlockingRecord[Grow];
s_blockingRecords ??= new BlockingRecord[Grow];
int found;
for (found = 0; found < s_nextBlockingRecordIndex; found++)
{
Expand Down
Loading

0 comments on commit 983c8f2

Please sign in to comment.