Skip to content

Commit

Permalink
Fix CA2213 (Disposable fields should be disposed) warnings (#3968)
Browse files Browse the repository at this point in the history
  • Loading branch information
kartheekp-ms authored Apr 3, 2021
1 parent 2069de3 commit 7e3b0e4
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 170 deletions.
5 changes: 2 additions & 3 deletions src/NuGet.Clients/NuGet.Console/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "NuGetConsole.Host.PowerShell")]
[assembly: SuppressMessage("Usage", "VSTHRD110:Observe result of async calls", Justification = "https://github.com/NuGet/Home/issues/7674", Scope = "member", Target = "~M:NuGetConsole.Implementation.Console.ConsoleDispatcher.Start")]
[assembly: SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Dispose method", Scope = "member", Target = "~M:NuGetConsole.ChannelOutputConsole.Dispose(System.Boolean)")]
[assembly: SuppressMessage("Build", "CA2213:'ChannelOutputConsole' contains field '_pipeLock' that is of IDisposable type 'AsyncSemaphore', but it is never disposed. Change the Dispose method on 'ChannelOutputConsole' to call Close or Dispose on this field.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGetConsole.ChannelOutputConsole._pipeLock")]
[assembly: SuppressMessage("Build", "CA2211:Non-constant fields should not be visible", Justification = "<Pending>", Scope = "member", Target = "~F:NuGetConsole.GuidList.guidNuGetOutputWindowPaneGuid")]
[assembly: SuppressMessage("Build", "CA2211:Non-constant fields should not be visible", Justification = "<Pending>", Scope = "member", Target = "~F:NuGetConsole.GuidList.guidVsWindowKindOutput")]
[assembly: SuppressMessage("Build", "CA2213:'WpfConsole' contains field '_marshaler' that is of IDisposable type 'PrivateMarshaler', but it is never disposed. Change the Dispose method on 'WpfConsole' to call Close or Dispose on this field.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGetConsole.Implementation.Console.WpfConsole._marshaler")]
[assembly: SuppressMessage("Build", "CA2213:'PowerConsoleWindow' contains field '_activeHostInfo' that is of IDisposable type 'HostInfo', but it is never disposed. Change the Dispose method on 'PowerConsoleWindow' to call Close or Dispose on this field.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGetConsole.Implementation.PowerConsole.PowerConsoleWindow._activeHostInfo")]
[assembly: SuppressMessage("Build", "CA2213:'WpfConsole' contains field '_marshaler' that is of IDisposable type 'PrivateMarshaler', but it is never disposed. Change the Dispose method on 'WpfConsole' to call Close or Dispose on this field.", Justification = "The Dispose() method on _marshaler is called when the tool window is closed.", Scope = "member", Target = "~F:NuGetConsole.Implementation.Console.WpfConsole._marshaler")]
[assembly: SuppressMessage("Build", "CA2213:'PowerConsoleWindow' contains field '_activeHostInfo' that is of IDisposable type 'HostInfo', but it is never disposed. Change the Dispose method on 'PowerConsoleWindow' to call Close or Dispose on this field.", Justification = "'_activeHostInfo' field holds reference to the first item in '_hostInfos' collection whose items are disposed.", Scope = "member", Target = "~F:NuGetConsole.Implementation.PowerConsole.PowerConsoleWindow._activeHostInfo")]
[assembly: SuppressMessage("Build", "CA1031:Modify 'CloseChannelAsync' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGetConsole.ChannelOutputConsole.CloseChannelAsync~System.Threading.Tasks.Task")]
[assembly: SuppressMessage("Build", "CA1031:Modify 'Dispose' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGetConsole.ChannelOutputConsole.Dispose(System.Boolean)")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'T CommonExtensionMethods.GetService<T>(IServiceProvider sp, Type serviceType)', validate parameter 'sp' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGetConsole.CommonExtensionMethods.GetService``1(System.IServiceProvider,System.Type)~``0")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ protected virtual void Dispose(bool disposing)
{
// Ignore exceptions
}

_pipeLock.Dispose();
}
_disposedValue = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@ internal class PowerConsoleWindow : IPowerConsoleWindow, IHostInitializer, IDisp
private Dictionary<string, HostInfo> _hostInfos;
private HostInfo _activeHostInfo;

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[Import(typeof(SVsServiceProvider))]
internal IServiceProvider ServiceProvider { get; set; }

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[Import]
internal IWpfConsoleService WpfConsoleService { get; set; }

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[ImportMany]
internal IEnumerable<Lazy<IHostProvider, IHostMetadata>> HostProviders { get; set; }

[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope",
Justification = "_hostInfo collection is disposed.")]
private Dictionary<string, HostInfo> HostInfos
{
get
Expand Down
5 changes: 0 additions & 5 deletions src/NuGet.Clients/NuGet.Console/WpfConsole/WpfConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,6 @@ public void ClearConsole()
}
}

[SuppressMessage(
"Microsoft.Usage",
"CA2213:DisposableFieldsShouldBeDisposed",
MessageId = "_marshaler",
Justification = "The Dispose() method on _marshaler is called when the tool window is closed.")]
[SuppressMessage(
"Microsoft.Design",
"CA1031:DoNotCatchGeneralExceptionTypes",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Usage", "VSTHRD003:Avoid awaiting foreign Tasks", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.UI.PackageLoadContext.GetInstalledPackagesAsync~System.Threading.Tasks.Task{NuGet.PackageManagement.VisualStudio.PackageCollection}")]
[assembly: SuppressMessage("Build", "CA2213:'PackageSourcesOptionsControl' contains field '_machineWidepackageSources' that is of IDisposable type 'BindingSource', but it is never disposed. Change the Dispose method on 'PackageSourcesOptionsControl' to call Close or Dispose on this field.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.Options.PackageSourcesOptionsControl._machineWidepackageSources")]
[assembly: SuppressMessage("Build", "CA2213:'PackageSourcesOptionsControl' contains field '_packageSources' that is of IDisposable type 'BindingSource', but it is never disposed. Change the Dispose method on 'PackageSourcesOptionsControl' to call Close or Dispose on this field.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.Options.PackageSourcesOptionsControl._packageSources")]
[assembly: SuppressMessage("Build", "CA2213:'PackageSourcesOptionsControl' contains field '_machineWidepackageSources' that is of IDisposable type 'BindingSource', but it is never disposed. Change the Dispose method on 'PackageSourcesOptionsControl' to call Close or Dispose on this field.", Justification = "'_machineWidepackageSources' field is disposed in 'PackageSourcesOptionsControl_Disposed' event handler", Scope = "member", Target = "~F:NuGet.Options.PackageSourcesOptionsControl._machineWidepackageSources")]
[assembly: SuppressMessage("Build", "CA2213:'PackageSourcesOptionsControl' contains field '_packageSources' that is of IDisposable type 'BindingSource', but it is never disposed. Change the Dispose method on 'PackageSourcesOptionsControl' to call Close or Dispose on this field.", Justification = "'_packageSources' field is disposed in 'PackageSourcesOptionsControl_Disposed' event handler", Scope = "member", Target = "~F:NuGet.Options.PackageSourcesOptionsControl._packageSources")]
[assembly: SuppressMessage("Build", "CA1051:Do not declare visible instance fields", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.PackageManagement.UI.DetailControlModel._projectVersionConstraints")]
[assembly: SuppressMessage("Build", "CA1051:Do not declare visible instance fields", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.PackageManagement.UI.DisplayVersion.AdditionalInfo")]
[assembly: SuppressMessage("Build", "CA1051:Do not declare visible instance fields", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.PackageManagement.UI.PreviewWindowModel.CopyGestures")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ private void PackageSourcesOptionsControl_Disposed(object sender, EventArgs e)
Disposed -= PackageSourcesOptionsControl_Disposed;
_nugetSourcesService?.Dispose();
_nugetSourcesService = null;
_packageSources?.Dispose();
_machineWidepackageSources?.Dispose();
}

private static bool IsPathRootedSafe(string path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

// This file is used by Code Analysis to maintain SuppressMessage
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;
Expand All @@ -14,8 +14,7 @@
[assembly: SuppressMessage("Usage", "VSTHRD011:Use AsyncLazy<T>", Justification = "https://github.com/NuGet/Home/issues/7675", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.VSSolutionManager.InitializeAsync~System.Threading.Tasks.Task")]
[assembly: SuppressMessage("Usage", "VSTHRD110:Observe result of async calls", Justification = "https://github.com/NuGet/Home/issues/7674", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.RuntimeHelpers.QueueUnloadAndForget(System.AppDomain)")]
[assembly: SuppressMessage("Usage", "VSTHRD003:Avoid awaiting foreign Tasks", Justification = "All these methods are free threaded.", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.MultiSourcePackageMetadataProvider.GetLocalPackageMetadataAsync(NuGet.Packaging.Core.PackageIdentity,System.Boolean,System.Threading.CancellationToken)~System.Threading.Tasks.Task{NuGet.Protocol.Core.Types.IPackageSearchMetadata}")]
[assembly: SuppressMessage("Build", "CA2213:'PackageFeedEnumerator' contains field '_current' that is of IDisposable type 'IEnumerator<IPackageSearchMetadata>', but it is never disposed. Change the Dispose method on 'PackageFeedEnumerator' to call Close or Dispose on this field.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.PackageManagement.VisualStudio.PackageFeedEnumerator._current")]
[assembly: SuppressMessage("Build", "CA2213:'ProjectRetargetingHandler' contains field '_errorListProvider' that is of IDisposable type 'ErrorListProvider', but it is never disposed. Change the Dispose method on 'ProjectRetargetingHandler' to call Close or Dispose on this field.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.PackageManagement.VisualStudio.ProjectRetargetingHandler._errorListProvider")]
[assembly: SuppressMessage("Build", "CA2213:'ProjectRetargetingHandler' contains field '_errorListProvider' that is of IDisposable type 'ErrorListProvider', but it is never disposed. Change the Dispose method on 'ProjectRetargetingHandler' to call Close or Dispose on this field.", Justification = "'_errorListProvider' field is disposed in the Dispose method after switching to the main thread in JTF context.", Scope = "member", Target = "~F:NuGet.PackageManagement.VisualStudio.ProjectRetargetingHandler._errorListProvider")]
[assembly: SuppressMessage("Build", "CA2211:Non-constant fields should not be visible", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.PackageManagement.VisualStudio.VSRestoreSettingsUtilities.AdditionalValue")]
[assembly: SuppressMessage("Build", "CA1802:Field 'VSNuGetClientName' is declared as 'readonly' but is initialized with a constant value. Mark this field as 'const' instead.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.PackageManagement.VisualStudio.VSSolutionManager.VSNuGetClientName")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'SearchPageTelemetryEvent.SearchPageTelemetryEvent(Guid parentId, int pageIndex, int resultCount, TimeSpan duration, IEnumerable<TimeSpan> sourceTimings, TimeSpan aggregationTime, LoadingStatus loadingStatus)', validate parameter 'sourceTimings' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.Telemetry.SearchPageTelemetryEvent.#ctor(System.Guid,System.Int32,System.Int32,System.TimeSpan,System.Collections.Generic.IEnumerable{System.TimeSpan},System.TimeSpan,NuGet.PackageManagement.VisualStudio.LoadingStatus)")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed class ProjectRetargetingHandler : IVsTrackProjectRetargetingEvents
private DTE _dte;
private ISolutionManager _solutionManager;
private IVsTrackProjectRetargeting _vsTrackProjectRetargeting;
private ErrorListProvider _errorListProvider;
private readonly ErrorListProvider _errorListProvider;
private IVsMonitorSelection _vsMonitorSelection;
private string _platformRetargetingProject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public sealed class PackageFeedEnumerator : IEnumerator<IPackageSearchMetadata>,
private Task<SearchResult<IPackageSearchMetadata>> _searchTask;
private IEnumerator<IPackageSearchMetadata> _current;

private bool _isDisposed;

private PackageFeedEnumerator(
IPackageFeed packageFeed,
Task<SearchResult<IPackageSearchMetadata>> searchTask,
Expand Down Expand Up @@ -72,6 +74,15 @@ private PackageFeedEnumerator(PackageFeedEnumerator other)

public void Dispose()
{
if (_isDisposed)
{
return;
}

_current?.Dispose();
GC.SuppressFinalize(this);

_isDisposed = true;
}

public bool MoveNext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "NuGetConsole.Host.PowerShell.Implementation")]
[assembly: SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "isSync", Scope = "member", Target = "NuGetConsole.Host.PowerShell.Implementation.PowerShellHost.#GetPathExpansionsAsyncCore(System.String,System.Boolean,System.Threading.CancellationToken)")]
[assembly: SuppressMessage("Usage", "VSTHRD110:Observe result of async calls", Justification = "https://github.com/NuGet/Home/issues/7674", Scope = "member", Target = "~M:NuGetConsole.Host.PowerShell.Implementation.PowerShellHost.HandleSolutionOpened")]
[assembly: SuppressMessage("Build", "CA2213:'PowerShellHost' contains field '_tokenSource' that is of IDisposable type 'CancellationTokenSource', but it is never disposed. Change the Dispose method on 'PowerShellHost' to call Close or Dispose on this field.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGetConsole.Host.PowerShell.Implementation.PowerShellHost._tokenSource")]
[assembly: SuppressMessage("Build", "CA1802:Field 'MaxTasks' is declared as 'readonly' but is initialized with a constant value. Mark this field as 'const' instead.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGetConsole.Host.PowerShell.Implementation.PowerShellHost.MaxTasks")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<SimpleExpansion> CommandExpansion.GetExpansionsAsync(string line, int caretIndex, CancellationToken token)', validate parameter 'line' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGetConsole.Host.CommandExpansion.GetExpansionsAsync(System.String,System.Int32,System.Threading.CancellationToken)~System.Threading.Tasks.Task{NuGetConsole.SimpleExpansion}")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'object MethodBinder.ChangeType(ParameterInfo parameterInfo, object arg)', validate parameter 'parameterInfo' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGetConsole.Host.MethodBinder.ChangeType(System.Reflection.ParameterInfo,System.Object)~System.Object")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ public void Dispose()
_restoreEvents.SolutionRestoreCompleted -= RestoreEvents_SolutionRestoreCompleted;
_initScriptsLock.Dispose();
Runspace?.Dispose();
_tokenSource?.Dispose();
}

#endregion
Expand Down
1 change: 0 additions & 1 deletion src/NuGet.Core/NuGet.Protocol/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Build", "CA1802:Field 'NuGetConfigurationKey' is declared as 'readonly' but is initialized with a constant value. Mark this field as 'const' instead.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.Protocol.HttpRequestMessageExtensions.NuGetConfigurationKey")]
[assembly: SuppressMessage("Build", "CA2213:'SymmetricHandshake' contains field '_timeoutCancellationTokenSource' that is of IDisposable type 'CancellationTokenSource', but it is never disposed. Change the Dispose method on 'SymmetricHandshake' to call Close or Dispose on this field.", Justification = "<Pending>", Scope = "member", Target = "~F:NuGet.Protocol.Plugins.SymmetricHandshake._timeoutCancellationTokenSource")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<Tuple<bool, INuGetResource>> AutoCompleteResourceV2FeedProvider.TryCreate(SourceRepository source, CancellationToken token)', validate parameter 'source' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.AutoCompleteResourceV2FeedProvider.TryCreate(NuGet.Protocol.Core.Types.SourceRepository,System.Threading.CancellationToken)~System.Threading.Tasks.Task{System.Tuple{System.Boolean,NuGet.Protocol.Core.Types.INuGetResource}}")]
[assembly: SuppressMessage("Build", "CA1308:In method 'IdStartsWith', replace the call to 'ToLowerInvariant' with 'ToUpperInvariant'.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.AutoCompleteResourceV3.IdStartsWith(System.String,System.Boolean,NuGet.Common.ILogger,System.Threading.CancellationToken)~System.Threading.Tasks.Task{System.Collections.Generic.IEnumerable{System.String}}")]
[assembly: SuppressMessage("Build", "CA1305:The behavior of 'bool.ToString()' could vary based on the current user's locale settings. Replace this call in 'AutoCompleteResourceV3.IdStartsWith(string, bool, ILogger, CancellationToken)' with a call to 'bool.ToString(IFormatProvider)'.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.AutoCompleteResourceV3.IdStartsWith(System.String,System.Boolean,NuGet.Common.ILogger,System.Threading.CancellationToken)~System.Threading.Tasks.Task{System.Collections.Generic.IEnumerable{System.String}}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Dispose()
}

_connection.MessageDispatcher.RequestHandlers.TryRemove(MessageMethod.Handshake);

_timeoutCancellationTokenSource.Dispose();
GC.SuppressFinalize(this);

_isDisposed = true;
Expand Down
Loading

0 comments on commit 7e3b0e4

Please sign in to comment.