Skip to content

Commit e55c908

Browse files
authored
Use null coalescing in many more places (#71361)
1 parent 9f7bf79 commit e55c908

File tree

641 files changed

+1768
-6410
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

641 files changed

+1768
-6410
lines changed

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ internal ILGenerator(MethodInfo methodBuilder, int size)
104104
internal virtual void RecordTokenFixup()
105105
{
106106
if (m_RelocFixupList == null)
107+
{
107108
m_RelocFixupList = new int[DefaultFixupArraySize];
109+
}
108110
else if (m_RelocFixupList.Length <= m_RelocFixupCount)
111+
{
109112
m_RelocFixupList = EnlargeArray(m_RelocFixupList);
113+
}
110114

111115
m_RelocFixupList[m_RelocFixupCount++] = m_length;
112116
}

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ internal static SignatureHelper GetMethodSigHelper(
5252
SignatureHelper sigHelp;
5353
MdSigCallingConvention intCall;
5454

55-
if (returnType == null)
56-
{
57-
returnType = typeof(void);
58-
}
55+
returnType ??= typeof(void);
5956

6057
intCall = MdSigCallingConvention.Default;
6158

@@ -150,10 +147,7 @@ public static SignatureHelper GetPropertySigHelper(Module? mod, CallingConventio
150147
{
151148
SignatureHelper sigHelp;
152149

153-
if (returnType == null)
154-
{
155-
returnType = typeof(void);
156-
}
150+
returnType ??= typeof(void);
157151

158152
MdSigCallingConvention intCall = MdSigCallingConvention.Property;
159153

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,10 +1430,7 @@ private ConstructorBuilder DefineDefaultConstructorNoLock(MethodAttributes attri
14301430
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, EmptyTypes, null);
14311431
}
14321432

1433-
if (con == null)
1434-
{
1435-
con = m_typeParent!.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, EmptyTypes, null);
1436-
}
1433+
con ??= m_typeParent!.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, EmptyTypes, null);
14371434

14381435
if (con == null)
14391436
throw new NotSupportedException(SR.NotSupported_NoParentDefaultConstructor);

src/libraries/Common/src/System/CodeDom/CodeTypeReference.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,7 @@ public CodeTypeReferenceCollection TypeArguments
348348
return ArrayElementType.TypeArguments;
349349
}
350350

351-
if (_typeArguments == null)
352-
{
353-
_typeArguments = new CodeTypeReferenceCollection();
354-
}
355-
356-
return _typeArguments;
351+
return _typeArguments ??= new CodeTypeReferenceCollection();
357352
}
358353
}
359354

src/libraries/Common/src/System/Net/ContextAwareResult.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,7 @@ private bool CaptureOrComplete(ref ExecutionContext? cachedContext, bool returnC
287287
{
288288
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, "starting capture");
289289

290-
if (cachedContext == null)
291-
{
292-
cachedContext = ExecutionContext.Capture();
293-
}
290+
cachedContext ??= ExecutionContext.Capture();
294291

295292
if (cachedContext != null)
296293
{

src/libraries/Common/src/System/Net/CookieParser.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,7 @@ private static FieldInfo IsQuotedVersionField
775775

776776
if (first && (token == CookieToken.NameValuePair || token == CookieToken.Attribute))
777777
{
778-
if (cookie == null)
779-
{
780-
cookie = new Cookie();
781-
}
778+
cookie ??= new Cookie();
782779
InternalSetNameMethod(cookie, _tokenizer.Name);
783780
cookie.Value = _tokenizer.Value;
784781
}

src/libraries/Common/src/System/Net/Logging/NetEventSource.Common.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,10 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string?
382382
{
383383
if (Log.IsEnabled())
384384
{
385-
if (arg1 == null) arg1 = "";
386-
if (arg2 == null) arg2 = "";
387-
if (arg3 == null) arg3 = "";
388-
if (arg4 == null) arg4 = "";
385+
arg1 ??= "";
386+
arg2 ??= "";
387+
arg3 ??= "";
388+
arg4 ??= "";
389389

390390
fixed (char* string1Bytes = arg1)
391391
fixed (char* string2Bytes = arg2)
@@ -430,9 +430,9 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, byte[]?
430430
{
431431
if (Log.IsEnabled())
432432
{
433-
if (arg1 == null) arg1 = "";
434-
if (arg2 == null) arg2 = "";
435-
if (arg3 == null) arg3 = Array.Empty<byte>();
433+
arg1 ??= "";
434+
arg2 ??= "";
435+
arg3 ??= Array.Empty<byte>();
436436

437437
fixed (char* arg1Ptr = arg1)
438438
fixed (char* arg2Ptr = arg2)
@@ -477,7 +477,7 @@ private unsafe void WriteEvent(int eventId, string? arg1, int arg2, int arg3, in
477477
{
478478
if (Log.IsEnabled())
479479
{
480-
if (arg1 == null) arg1 = "";
480+
arg1 ??= "";
481481

482482
fixed (char* arg1Ptr = arg1)
483483
{
@@ -519,8 +519,8 @@ private unsafe void WriteEvent(int eventId, string? arg1, int arg2, string? arg3
519519
{
520520
if (Log.IsEnabled())
521521
{
522-
if (arg1 == null) arg1 = "";
523-
if (arg3 == null) arg3 = "";
522+
arg1 ??= "";
523+
arg3 ??= "";
524524

525525
fixed (char* arg1Ptr = arg1)
526526
fixed (char* arg3Ptr = arg3)
@@ -558,8 +558,8 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, int arg3
558558
{
559559
if (Log.IsEnabled())
560560
{
561-
if (arg1 == null) arg1 = "";
562-
if (arg2 == null) arg2 = "";
561+
arg1 ??= "";
562+
arg2 ??= "";
563563

564564
fixed (char* arg1Ptr = arg1)
565565
fixed (char* arg2Ptr = arg2)
@@ -597,9 +597,9 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string?
597597
{
598598
if (Log.IsEnabled())
599599
{
600-
if (arg1 == null) arg1 = "";
601-
if (arg2 == null) arg2 = "";
602-
if (arg3 == null) arg3 = "";
600+
arg1 ??= "";
601+
arg2 ??= "";
602+
arg3 ??= "";
603603

604604
fixed (char* arg1Ptr = arg1)
605605
fixed (char* arg2Ptr = arg2)
@@ -643,7 +643,7 @@ private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int
643643
{
644644
if (Log.IsEnabled())
645645
{
646-
if (arg1 == null) arg1 = "";
646+
arg1 ??= "";
647647

648648
fixed (char* arg1Ptr = arg1)
649649
{
@@ -705,8 +705,8 @@ private unsafe void WriteEvent(int eventId, string arg1, string arg2, int arg3,
705705
{
706706
if (Log.IsEnabled())
707707
{
708-
if (arg1 == null) arg1 = "";
709-
if (arg2 == null) arg2 = "";
708+
arg1 ??= "";
709+
arg2 ??= "";
710710

711711
fixed (char* arg1Ptr = arg1)
712712
fixed (char* arg2Ptr = arg2)

src/libraries/Common/src/System/Net/NTAuthentication.Common.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,7 @@ internal sealed partial class NTAuthentication
3939
// True indicates this instance is for Server and will use AcceptSecurityContext SSPI API.
4040
internal bool IsServer => _isServer;
4141

42-
internal string? ClientSpecifiedSpn
43-
{
44-
get
45-
{
46-
if (_clientSpecifiedSpn == null)
47-
{
48-
_clientSpecifiedSpn = GetClientSpecifiedSpn();
49-
}
50-
51-
return _clientSpecifiedSpn;
52-
}
53-
}
42+
internal string? ClientSpecifiedSpn => _clientSpecifiedSpn ??= GetClientSpecifiedSpn();
5443

5544
internal string ProtocolName
5645
{

src/libraries/Common/src/System/Net/Security/NegotiateStreamPal.Unix.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,7 @@ internal static SecurityStatusPal AcceptSecurityContext(
385385
ref byte[] resultBlob,
386386
ref ContextFlagsPal contextFlags)
387387
{
388-
if (securityContext == null)
389-
{
390-
securityContext = new SafeDeleteNegoContext((SafeFreeNegoCredentials)credentialsHandle!);
391-
}
388+
securityContext ??= new SafeDeleteNegoContext((SafeFreeNegoCredentials)credentialsHandle!);
392389

393390
SafeDeleteNegoContext negoContext = (SafeDeleteNegoContext)securityContext;
394391
try

src/libraries/Common/src/System/Resources/ResourceWriter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ private void AddResourceData(string name, string typeName, object data)
196196

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

202201
_preserializedData.Add(name, new PrecannedResource(typeName, data));
203202
}

0 commit comments

Comments
 (0)