Skip to content

Commit

Permalink
Use null coalescing in many more places (#71361)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Jun 28, 2022
1 parent 9f7bf79 commit e55c908
Show file tree
Hide file tree
Showing 641 changed files with 1,768 additions and 6,410 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ internal ILGenerator(MethodInfo methodBuilder, int size)
internal virtual void RecordTokenFixup()
{
if (m_RelocFixupList == null)
{
m_RelocFixupList = new int[DefaultFixupArraySize];
}
else if (m_RelocFixupList.Length <= m_RelocFixupCount)
{
m_RelocFixupList = EnlargeArray(m_RelocFixupList);
}

m_RelocFixupList[m_RelocFixupCount++] = m_length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ internal static SignatureHelper GetMethodSigHelper(
SignatureHelper sigHelp;
MdSigCallingConvention intCall;

if (returnType == null)
{
returnType = typeof(void);
}
returnType ??= typeof(void);

intCall = MdSigCallingConvention.Default;

Expand Down Expand Up @@ -150,10 +147,7 @@ public static SignatureHelper GetPropertySigHelper(Module? mod, CallingConventio
{
SignatureHelper sigHelp;

if (returnType == null)
{
returnType = typeof(void);
}
returnType ??= typeof(void);

MdSigCallingConvention intCall = MdSigCallingConvention.Property;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1430,10 +1430,7 @@ private ConstructorBuilder DefineDefaultConstructorNoLock(MethodAttributes attri
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, EmptyTypes, null);
}

if (con == null)
{
con = m_typeParent!.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, EmptyTypes, null);
}
con ??= m_typeParent!.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, EmptyTypes, null);

if (con == null)
throw new NotSupportedException(SR.NotSupported_NoParentDefaultConstructor);
Expand Down
7 changes: 1 addition & 6 deletions src/libraries/Common/src/System/CodeDom/CodeTypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,7 @@ public CodeTypeReferenceCollection TypeArguments
return ArrayElementType.TypeArguments;
}

if (_typeArguments == null)
{
_typeArguments = new CodeTypeReferenceCollection();
}

return _typeArguments;
return _typeArguments ??= new CodeTypeReferenceCollection();
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/libraries/Common/src/System/Net/ContextAwareResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,7 @@ private bool CaptureOrComplete(ref ExecutionContext? cachedContext, bool returnC
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, "starting capture");

if (cachedContext == null)
{
cachedContext = ExecutionContext.Capture();
}
cachedContext ??= ExecutionContext.Capture();

if (cachedContext != null)
{
Expand Down
5 changes: 1 addition & 4 deletions src/libraries/Common/src/System/Net/CookieParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,7 @@ private static FieldInfo IsQuotedVersionField

if (first && (token == CookieToken.NameValuePair || token == CookieToken.Attribute))
{
if (cookie == null)
{
cookie = new Cookie();
}
cookie ??= new Cookie();
InternalSetNameMethod(cookie, _tokenizer.Name);
cookie.Value = _tokenizer.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,10 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string?
{
if (Log.IsEnabled())
{
if (arg1 == null) arg1 = "";
if (arg2 == null) arg2 = "";
if (arg3 == null) arg3 = "";
if (arg4 == null) arg4 = "";
arg1 ??= "";
arg2 ??= "";
arg3 ??= "";
arg4 ??= "";

fixed (char* string1Bytes = arg1)
fixed (char* string2Bytes = arg2)
Expand Down Expand Up @@ -430,9 +430,9 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, byte[]?
{
if (Log.IsEnabled())
{
if (arg1 == null) arg1 = "";
if (arg2 == null) arg2 = "";
if (arg3 == null) arg3 = Array.Empty<byte>();
arg1 ??= "";
arg2 ??= "";
arg3 ??= Array.Empty<byte>();

fixed (char* arg1Ptr = arg1)
fixed (char* arg2Ptr = arg2)
Expand Down Expand Up @@ -477,7 +477,7 @@ private unsafe void WriteEvent(int eventId, string? arg1, int arg2, int arg3, in
{
if (Log.IsEnabled())
{
if (arg1 == null) arg1 = "";
arg1 ??= "";

fixed (char* arg1Ptr = arg1)
{
Expand Down Expand Up @@ -519,8 +519,8 @@ private unsafe void WriteEvent(int eventId, string? arg1, int arg2, string? arg3
{
if (Log.IsEnabled())
{
if (arg1 == null) arg1 = "";
if (arg3 == null) arg3 = "";
arg1 ??= "";
arg3 ??= "";

fixed (char* arg1Ptr = arg1)
fixed (char* arg3Ptr = arg3)
Expand Down Expand Up @@ -558,8 +558,8 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, int arg3
{
if (Log.IsEnabled())
{
if (arg1 == null) arg1 = "";
if (arg2 == null) arg2 = "";
arg1 ??= "";
arg2 ??= "";

fixed (char* arg1Ptr = arg1)
fixed (char* arg2Ptr = arg2)
Expand Down Expand Up @@ -597,9 +597,9 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string?
{
if (Log.IsEnabled())
{
if (arg1 == null) arg1 = "";
if (arg2 == null) arg2 = "";
if (arg3 == null) arg3 = "";
arg1 ??= "";
arg2 ??= "";
arg3 ??= "";

fixed (char* arg1Ptr = arg1)
fixed (char* arg2Ptr = arg2)
Expand Down Expand Up @@ -643,7 +643,7 @@ private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int
{
if (Log.IsEnabled())
{
if (arg1 == null) arg1 = "";
arg1 ??= "";

fixed (char* arg1Ptr = arg1)
{
Expand Down Expand Up @@ -705,8 +705,8 @@ private unsafe void WriteEvent(int eventId, string arg1, string arg2, int arg3,
{
if (Log.IsEnabled())
{
if (arg1 == null) arg1 = "";
if (arg2 == null) arg2 = "";
arg1 ??= "";
arg2 ??= "";

fixed (char* arg1Ptr = arg1)
fixed (char* arg2Ptr = arg2)
Expand Down
13 changes: 1 addition & 12 deletions src/libraries/Common/src/System/Net/NTAuthentication.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,7 @@ internal sealed partial class NTAuthentication
// True indicates this instance is for Server and will use AcceptSecurityContext SSPI API.
internal bool IsServer => _isServer;

internal string? ClientSpecifiedSpn
{
get
{
if (_clientSpecifiedSpn == null)
{
_clientSpecifiedSpn = GetClientSpecifiedSpn();
}

return _clientSpecifiedSpn;
}
}
internal string? ClientSpecifiedSpn => _clientSpecifiedSpn ??= GetClientSpecifiedSpn();

internal string ProtocolName
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,7 @@ internal static SecurityStatusPal AcceptSecurityContext(
ref byte[] resultBlob,
ref ContextFlagsPal contextFlags)
{
if (securityContext == null)
{
securityContext = new SafeDeleteNegoContext((SafeFreeNegoCredentials)credentialsHandle!);
}
securityContext ??= new SafeDeleteNegoContext((SafeFreeNegoCredentials)credentialsHandle!);

SafeDeleteNegoContext negoContext = (SafeDeleteNegoContext)securityContext;
try
Expand Down
3 changes: 1 addition & 2 deletions src/libraries/Common/src/System/Resources/ResourceWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ private void AddResourceData(string name, string typeName, object data)

// Check for duplicate resources whose names vary only by case.
_caseInsensitiveDups.Add(name, null);
if (_preserializedData == null)
_preserializedData = new Dictionary<string, PrecannedResource>(FastResourceComparer.Default);
_preserializedData ??= new Dictionary<string, PrecannedResource>(FastResourceComparer.Default);

_preserializedData.Add(name, new PrecannedResource(typeName, data));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ private void PreProcessSignature()
&& pi.ParameterType.HasElementType
&& pi.ParameterType.GetElementType()!.IsEnum)
{
if (targetTypes == null)
{
targetTypes = new Type?[_expectedParamsCount];
}
targetTypes ??= new Type?[_expectedParamsCount];

targetTypes[i] = pi.ParameterType.GetElementType();
}
Expand Down
12 changes: 1 addition & 11 deletions src/libraries/Common/src/System/Text/OSEncoding.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,7 @@ public override int GetMaxCharCount(int byteCount)
return (int)charCount;
}

public override string EncodingName
{
get
{
if (_encodingName == null)
{
_encodingName = "Codepage - " + _codePage.ToString();
}
return _encodingName;
}
}
public override string EncodingName => _encodingName ??= $"Codepage - {_codePage}";

public override string WebName
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ private void Initialize(object rcw, Guid iid)
[RequiresUnreferencedCode(Binder.TrimmerWarning)]
public void AddHandler(int dispid, object func)
{
ComEventsMethod method = FindMethod(dispid);
if (method == null)
{
method = AddMethod(dispid);
}
ComEventsMethod method = FindMethod(dispid) ?? AddMethod(dispid);

if (func is Delegate d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,7 @@ private ParameterExpression PropertyPutDispIdVariable
get { return EnsureVariable(ref _propertyPutDispId, typeof(int), "propertyPutDispId"); }
}

private ParameterExpression ParamVariantsVariable
{
get
{
if (_paramVariants == null)
{
_paramVariants = Expression.Variable(VariantArray.GetStructType(_args.Length), "paramVariants");
}
return _paramVariants;
}
}
private ParameterExpression ParamVariantsVariable => _paramVariants ??= Expression.Variable(VariantArray.GetStructType(_args.Length), "paramVariants");

private static ParameterExpression EnsureVariable(ref ParameterExpression var, Type type, string name)
{
Expand All @@ -140,10 +130,7 @@ private static Type MarshalType(DynamicMetaObject mo, bool isByRef)
if (isByRef)
{
// Null just means that null was supplied.
if (marshalType == null)
{
marshalType = mo.Expression.Type;
}
marshalType ??= mo.Expression.Type;
marshalType = marshalType.MakeByRefType();
}
return marshalType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ internal sealed class ComTypeClassDesc : ComTypeDesc, IDynamicMetaObjectProvider
[RequiresUnreferencedCode(Binder.TrimmerWarning)]
public object CreateInstance()
{
if (_typeObj == null)
{
_typeObj = Type.GetTypeFromCLSID(Guid);
}
_typeObj ??= Type.GetTypeFromCLSID(Guid);
return Activator.CreateInstance(Type.GetTypeFromCLSID(Guid));
}

Expand Down Expand Up @@ -50,19 +47,12 @@ private void AddInterface(ComTypes.ITypeInfo itfTypeInfo, bool isSourceItf)

if (isSourceItf)
{
if (_sourceItfs == null)
{
_sourceItfs = new LinkedList<string>();
}
_sourceItfs ??= new LinkedList<string>();
_sourceItfs.AddLast(itfName);
}
else
{
if (_itfs == null)
{
_itfs = new LinkedList<string>();
}

_itfs ??= new LinkedList<string>();
_itfs.AddLast(itfName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,7 @@ internal override IList<string> GetMemberNames(bool dataOnly)
[RequiresUnreferencedCode(Binder.TrimmerWarning)]
internal override IList<KeyValuePair<string, object>> GetMembers(IEnumerable<string> names)
{
if (names == null)
{
names = GetMemberNames(true);
}
names ??= GetMemberNames(true);

Type comType = RuntimeCallableWrapper.GetType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ internal object Invoke(object[] args)
Debug.Assert(args != null);

// Create a CallSite and invoke it.
if (_site == null)
{
_site = CallSite<Func<CallSite, object, object[], object>>.Create(SplatInvokeBinder.Instance);
}
_site ??= CallSite<Func<CallSite, object, object[], object>>.Create(SplatInvokeBinder.Instance);

return _site.Target(_site, _callable, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@ internal static MethodInfo GetUserDefinedCoercionMethod(Type convertFrom, Type c
// try lifted conversion
if (nnExprType != convertFrom || nnConvType != convertToType)
{
method = FindConversionOperator(eMethods, nnExprType, nnConvType, implicitOnly);
if (method == null)
{
method = FindConversionOperator(cMethods, nnExprType, nnConvType, implicitOnly);
}
method =
FindConversionOperator(eMethods, nnExprType, nnConvType, implicitOnly) ??
FindConversionOperator(cMethods, nnExprType, nnConvType, implicitOnly);
if (method != null)
{
return method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ internal struct UserStringBuilder
private void BeginString()
{
Debug.Assert(_strBuilder == null || _strBuilder.Length == 0);
if (_strBuilder == null)
{
_strBuilder = new StringBuilder();
}
_strBuilder ??= new StringBuilder();
}

private string EndString()
Expand Down
Loading

0 comments on commit e55c908

Please sign in to comment.