Skip to content

Commit 54a230f

Browse files
authored
Simplify null checks to fix IDE0031 (#119722)
Don't set severity for dotnet_style_null_propagation so the value in eng/CodeAnalysis.src.globalconfig takes precedence Fixes #119391
1 parent b54a16e commit 54a230f

File tree

79 files changed

+143
-430
lines changed

Some content is hidden

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

79 files changed

+143
-430
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ dotnet_style_collection_initializer = true:suggestion
9999
dotnet_style_prefer_collection_expression = when_types_exactly_match
100100
dotnet_style_explicit_tuple_names = true:suggestion
101101
dotnet_style_coalesce_expression = true:suggestion
102-
dotnet_style_null_propagation = true:suggestion
102+
dotnet_style_null_propagation = true
103103
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
104104
dotnet_style_prefer_inferred_tuple_names = true:suggestion
105105
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.CoreCLR.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ private void InvokeClassConstructor()
221221
public override MethodBody? GetMethodBody()
222222
{
223223
RuntimeMethodBody? mb = RuntimeMethodHandle.GetMethodBody(this, ReflectedTypeInternal);
224-
if (mb != null)
225-
mb._methodBase = this;
224+
mb?._methodBase = this;
226225
return mb;
227226
}
228227

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,7 @@ public override MethodImplAttributes GetMethodImplementationFlags()
274274
public override MethodBody? GetMethodBody()
275275
{
276276
RuntimeMethodBody? mb = RuntimeMethodHandle.GetMethodBody(this, ReflectedTypeInternal);
277-
if (mb != null)
278-
mb._methodBase = this;
277+
mb?._methodBase = this;
279278
return mb;
280279
}
281280

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Condition.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ private unsafe void AddWaiter(Waiter waiter)
6262
AssertIsNotInList(waiter);
6363

6464
waiter.prev = _waitersTail;
65-
if (waiter.prev != null)
66-
waiter.prev.next = waiter;
65+
waiter.prev?.next = waiter;
6766

6867
_waitersTail = waiter;
6968

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeLoaderEnvironment.ConstructedGenericsRegistration.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,16 @@ internal void RegisterDynamicGenericTypesAndMethods(DynamicGenericsRegistrationD
102102
var typeEntry = registeredTypes[i];
103103
// There is no Remove feature in the LockFreeReaderHashtable...
104104
GenericTypeEntry failedEntry = _dynamicGenericTypes.GetValueIfExists(typeEntry);
105-
if (failedEntry != null)
106-
failedEntry._isRegisteredSuccessfully = false;
105+
failedEntry?._isRegisteredSuccessfully = false;
107106
}
108107
for (int i = 0; i < registeredMethodsCount; i++)
109108
{
110109
// There is no Remove feature in the LockFreeReaderHashtable...
111110
GenericMethodEntry failedEntry = _dynamicGenericMethods.GetValueIfExists(registeredMethods[i]);
112-
if (failedEntry != null)
113-
failedEntry._isRegisteredSuccessfully = false;
111+
failedEntry?._isRegisteredSuccessfully = false;
114112

115113
failedEntry = _dynamicGenericMethodComponents.GetValueIfExists(registeredMethods[i]);
116-
if (failedEntry != null)
117-
failedEntry._isRegisteredSuccessfully = false;
114+
failedEntry?._isRegisteredSuccessfully = false;
118115
}
119116
}
120117
catch (Exception e)

src/libraries/Common/src/System/Runtime/InteropServices/ComEventsMethod.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,7 @@ public static ComEventsMethod Add(ComEventsMethod? methods, ComEventsMethod meth
144144
current = current._next;
145145
}
146146

147-
if (current != null)
148-
{
149-
current._next = method._next;
150-
}
147+
current?._next = method._next;
151148

152149
return methods;
153150
}

src/libraries/Common/src/System/Runtime/InteropServices/ComEventsSink.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ public static ComEventsSink Add(ComEventsSink? sinks, ComEventsSink sink)
7373
current = current._next;
7474
}
7575

76-
if (current != null)
77-
{
78-
current._next = sink._next;
79-
}
76+
current?._next = sink._next;
8077
}
8178

8279
sink.Unadvise();

src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,10 +682,7 @@ private void ReplaceNode(Node match, Node parentOfMatch, Node successor, Node pa
682682
successor.Left = match.Left;
683683
}
684684

685-
if (successor != null)
686-
{
687-
successor.Color = match.Color;
688-
}
685+
successor?.Color = match.Color;
689686

690687
ReplaceChildOrRoot(parentOfMatch, match, successor!);
691688
}

src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/AtomicComposition.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,7 @@ protected virtual void Dispose(bool disposing)
137137
ThrowIfDisposed();
138138
_isDisposed = true;
139139

140-
if (_outerAtomicComposition != null)
141-
{
142-
_outerAtomicComposition.ContainsInnerAtomicComposition = false;
143-
}
140+
_outerAtomicComposition?.ContainsInnerAtomicComposition = false;
144141

145142
// Revert is always immediate and involves forgetting information and
146143
// exceuting any appropriate revert actions

src/libraries/System.ComponentModel.Primitives/src/System/ComponentModel/EventHandlerList.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ public void AddHandlers(EventHandlerList listToAddFrom)
100100
public void RemoveHandler(object key, Delegate? value)
101101
{
102102
ListEntry? e = Find(key);
103-
if (e != null)
104-
{
105-
e._handler = Delegate.Remove(e._handler, value);
106-
}
103+
e?._handler = Delegate.Remove(e._handler, value);
107104
}
108105

109106
private sealed class ListEntry

0 commit comments

Comments
 (0)