Skip to content

Commit

Permalink
Enable and refactor to fix IDE0031 Use null propagation (dotnet#7902)
Browse files Browse the repository at this point in the history
  • Loading branch information
elachlan authored and v-elnovikova committed Oct 18, 2022
1 parent 276e13f commit ed76a3f
Show file tree
Hide file tree
Showing 145 changed files with 487 additions and 1,828 deletions.
2 changes: 1 addition & 1 deletion eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ dotnet_diagnostic.IDE0029.severity = warning
dotnet_diagnostic.IDE0030.severity = warning

# IDE0031: Use null propagation
dotnet_diagnostic.IDE0031.severity = none # TODO: warning
dotnet_diagnostic.IDE0031.severity = warning

# IDE0032: Use auto property
dotnet_diagnostic.IDE0032.severity = silent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices
_processingUnhandledExceptionEvent = True

For Each handler As UnhandledExceptionEventHandler In _unhandledExceptionHandlers
If handler IsNot Nothing Then
handler.Invoke(sender, e)
End If
handler?.Invoke(sender, e)
Next

' Now that we are out of the unhandled exception handler, treat exceptions normally again.
Expand Down Expand Up @@ -747,9 +745,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices
' Dispose on the Splash screen. (we're just swapping the order of the two If blocks.)
' This is to fix the issue where the main form doesn't come to the front after the
' Splash screen disappears.
If MainForm IsNot Nothing Then
MainForm.Activate()
End If
MainForm?.Activate()

If _splashScreen IsNot Nothing AndAlso Not _splashScreen.IsDisposed Then
Dim disposeSplashDelegate As New DisposeDelegate(AddressOf _splashScreen.Dispose)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ Namespace Microsoft.VisualBasic
Debug.Assert([Enum].IsDefined(GetType(AudioPlayMode), mode), "Enum value is out of range")

' Stopping the sound ensures it's safe to dispose it. This could happen when we change the value of m_Sound below
If _sound IsNot Nothing Then
_sound.Stop()
End If
_sound?.Stop()

_sound = sound

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ Namespace Microsoft.VisualBasic.CompilerServices

Protected Overloads Overrides Sub Dispose(disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
components?.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,7 @@ Namespace Microsoft.VisualBasic.Logging
''' Flushes the underlying stream
''' </summary>
Public Overrides Sub Flush()
If _stream IsNot Nothing Then
_stream.Flush()
End If
_stream?.Flush()
End Sub

''' <summary>
Expand Down Expand Up @@ -894,9 +892,7 @@ Namespace Microsoft.VisualBasic.Logging
Return Reader.CurrentEncoding
End If
Finally
If Reader IsNot Nothing Then
Reader.Close()
End If
Reader?.Close()
End Try
End If

Expand Down Expand Up @@ -1226,9 +1222,7 @@ Namespace Microsoft.VisualBasic.Logging
Private Overloads Sub Dispose(disposing As Boolean)
If disposing Then
If Not _disposed Then
If _stream IsNot Nothing Then
_stream.Close()
End If
_stream?.Close()
_disposed = True
End If
End If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,7 @@ Namespace Microsoft.VisualBasic.MyServices.Internal
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(disposing As Boolean)
If disposing Then
If Not (_components Is Nothing) Then
_components.Dispose()
End If
_components?.Dispose()
If _formClosableSemaphore IsNot Nothing Then
_formClosableSemaphore.Dispose()
_formClosableSemaphore = Nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,7 @@ public void CloseServerAfterClientConnect()
}
finally
{
if (pipeClient != null)
{
pipeClient.Dispose();
}
pipeClient?.Dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,7 @@ public static byte[] AttachCorrelationId(byte[] buffer, Guid correlationId)
byte[] bufferWithCorrelation = new byte[s_correlationMarkBytes.Length + correlationIdBytes.Length + (buffer is not null ? buffer.Length : 0)];
s_correlationMarkBytes.CopyTo(bufferWithCorrelation, 0);
correlationIdBytes.CopyTo(bufferWithCorrelation, s_correlationMarkBytes.Length);
if (buffer is not null)
{
buffer.CopyTo(bufferWithCorrelation, s_correlationMarkBytes.Length + correlationIdBytes.Length);
}
buffer?.CopyTo(bufferWithCorrelation, s_correlationMarkBytes.Length + correlationIdBytes.Length);

return bufferWithCorrelation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,7 @@ private void CancelButton_click(object sender, EventArgs e)
_createdItems.Clear();
}

if (_removedItems is not null)
{
_removedItems.Clear();
}
_removedItems?.Clear();

// Restore the original contents. Because objects get parented during CreateAndAddInstance, the underlying collection
// gets changed during add, but not other operations. Not all consumers of this dialog can roll back every single change,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,7 @@ protected virtual void Dispose(bool disposing)
{
try
{
if (_host is not null)
{
_host.DisposeHost();
}
_host?.DisposeHost();
}
finally
{
Expand All @@ -409,10 +406,7 @@ protected virtual void Dispose(bool disposing)
/// </summary>
public void Flush()
{
if (_host is not null)
{
_host.Flush();
}
_host?.Flush();

Flushed?.Invoke(this, EventArgs.Empty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,7 @@ public DesignSurface CreateDesignSurface(IServiceProvider parentProvider)
// the ones providing the event service, then whoever is providing
// it will be responsible for updating it when new designers are created.
DesignerEventService eventService = GetService(typeof(IDesignerEventService)) as DesignerEventService;
if (eventService is not null)
{
eventService.OnCreateDesigner(surface);
}
eventService?.OnCreateDesigner(surface);

return surface;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,18 +792,12 @@ protected override void OnPaint(PaintEventArgs e)
}
finally
{
if (attrs is not null)
{
attrs.Dispose();
}
attrs?.Dispose();
}
}
finally
{
if (icon is not null)
{
icon.Dispose();
}
icon?.Dispose();
}
}
catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,7 @@ private void OnVerbStatusChanged(object sender, EventArgs args)
if (verb == sender)
{
DesignerActionUIService dapUISvc = (DesignerActionUIService)sc.GetService(typeof(DesignerActionUIService));
if (dapUISvc is not null)
{
dapUISvc.Refresh(comp); // we need to refresh, a verb on the current panel has changed its state
}
dapUISvc?.Refresh(comp); // we need to refresh, a verb on the current panel has changed its state
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ public void Dispose()
if (_cmdShowDesignerActions is not null)
{
IMenuCommandService mcs = (IMenuCommandService)_serviceProvider.GetService(typeof(IMenuCommandService));
if (mcs is not null)
{
mcs.RemoveCommand(_cmdShowDesignerActions);
}
mcs?.RemoveCommand(_cmdShowDesignerActions);
}
}

Expand Down Expand Up @@ -568,10 +565,7 @@ private void InvalidateGlyphOnLastTransaction(object sender, DesignerTransaction
host.TransactionClosed -= new DesignerTransactionCloseEventHandler(InvalidateGlyphOnLastTransaction);
}

if (_relatedGlyphTransaction is not null)
{
_relatedGlyphTransaction.InvalidateOwnerLocation();
}
_relatedGlyphTransaction?.InvalidateOwnerLocation();

_relatedGlyphTransaction = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ public void Dispose()
if (_serviceProvider is not null)
{
IDesignerHost host = (IDesignerHost)_serviceProvider.GetService(typeof(IDesignerHost));
if (host is not null)
{
host.RemoveService(typeof(DesignerActionUIService));
}
host?.RemoveService(typeof(DesignerActionUIService));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ private void OnDesignerDisposed(object sender, EventArgs e)
}
}

if (_designerList is not null)
{
_designerList.Remove(host);
}
_designerList?.Remove(host);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,7 @@ protected override ISite CreateSite(IComponent component, string name)
}
else
{
if (nameCreate is not null)
{
nameCreate.ValidateName(name);
}
nameCreate?.ValidateName(name);
}

return new Site(component, this, name, this);
Expand Down Expand Up @@ -472,10 +469,7 @@ internal void DisposeHost()
/// </summary>
internal void Flush()
{
if (_loader is not null)
{
_loader.Flush();
}
_loader?.Flush();
}

/// <summary>
Expand Down Expand Up @@ -706,10 +700,7 @@ private void Unload()
}

ISelectionService selectionService = (ISelectionService)GetService(typeof(ISelectionService));
if (selectionService is not null)
{
selectionService.SetSelectedComponents(null, SelectionTypes.Replace);
}
selectionService?.SetSelectedComponents(null, SelectionTypes.Replace);

// Now remove all the designers and their components. We save the root for last. Note that we eat any exceptions that components or their designers generate. A bad component or designer should not prevent an unload from happening. We do all of this in a transaction to help reduce the number of events we generate.
_state[s_stateUnloading] = true;
Expand Down Expand Up @@ -1251,10 +1242,7 @@ void IDesignerLoaderHost.EndLoad(string rootClassName, bool successful, ICollect
errorCollection = errorList;
successful = false;

if (_surface is not null)
{
_surface.OnLoaded(successful, errorCollection);
}
_surface?.OnLoaded(successful, errorCollection);

// We re-throw. If this was a synchronous load this will error back to BeginLoad (and, as a side effect, may call us again). For asynchronous loads we need to throw so the caller knows what happened.
throw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ private void OnComponentAdded(object sender, ComponentEventArgs cevent)
if (!(compAdded.Site is INestedSite))
{
_addedComponents.Add(compAdded);
if (_removedComponents is not null)
{
_removedComponents.Remove(compAdded);
}
_removedComponents?.Remove(compAdded);
}
}

Expand All @@ -161,10 +158,7 @@ private void OnComponentRemoved(object sender, ComponentEventArgs cevent)
if (!(compRemoved.Site is INestedSite))
{
_removedComponents.Add(compRemoved);
if (_addedComponents is not null)
{
_addedComponents.Remove(compRemoved);
}
_addedComponents?.Remove(compRemoved);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,7 @@ internal object PrimarySelection
/// </summary>
internal void RemoveSelection(object sel)
{
if (_selection is not null)
{
_selection.Remove(sel);
}
_selection?.Remove(sel);
}

private void ApplicationIdle(object source, EventArgs args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,7 @@ private static void DeserializeDesignTimeProperties(IDesignerSerializationManage
foreach (DictionaryEntry de in (IDictionary)state)
{
PropertyDescriptor prop = props[(string)de.Key];
if (prop is not null)
{
prop.SetValue(comp, de.Value);
}
prop?.SetValue(comp, de.Value);
}
}
}
Expand Down Expand Up @@ -1051,10 +1048,7 @@ private static void DeserializeEventResets(IDesignerSerializationManager manager
{
PropertyDescriptor prop = eventProps[eventName];

if (prop is not null)
{
prop.SetValue(comp, null);
}
prop?.SetValue(comp, null);
}
}
}
Expand All @@ -1069,10 +1063,7 @@ private static void DeserializeModifier(IDesignerSerializationManager manager, s
{
MemberAttributes modifierValue = (MemberAttributes)state;
PropertyDescriptor modifierProp = TypeDescriptor.GetProperties(comp)["Modifiers"];
if (modifierProp is not null)
{
modifierProp.SetValue(comp, modifierValue);
}
modifierProp?.SetValue(comp, modifierValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ public static void SetGenerateMember(IComponent comp, bool generate)
IDictionaryService dictionary = (IDictionaryService)site.GetService(typeof(IDictionaryService));
bool oldValue = GetGenerateMember(comp);

if (dictionary is not null)
{
dictionary.SetValue("GenerateMember", generate);
}
dictionary?.SetValue("GenerateMember", generate);

// If the old value was true and the new value is false, we've got
// to remove the existing member declaration for this
Expand Down Expand Up @@ -219,10 +216,7 @@ public static void SetModifiers(IComponent comp, MemberAttributes modifiers)

IDictionaryService dictionary = (IDictionaryService)site.GetService(typeof(IDictionaryService));

if (dictionary is not null)
{
dictionary.SetValue("Modifiers", modifiers);
}
dictionary?.SetValue("Modifiers", modifiers);
}
}
}
Expand Down
Loading

0 comments on commit ed76a3f

Please sign in to comment.