Enable CA1816: Dispose methods should call SuppressFinalize (#14074)

https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1816
This commit is contained in:
xtqqczze 2020-11-24 04:29:15 +00:00 committed by GitHub
parent cb6ef9bdee
commit 358db74479
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 14 deletions

View file

@ -268,7 +268,7 @@ dotnet_diagnostic.CA1814.severity = none
dotnet_diagnostic.CA1815.severity = none
# CA1816: Dispose methods should call SuppressFinalize
dotnet_diagnostic.CA1816.severity = suggestion
dotnet_diagnostic.CA1816.severity = warning
# CA1819: Properties should not return arrays
dotnet_diagnostic.CA1819.severity = none

View file

@ -101,11 +101,28 @@ namespace Microsoft.PowerShell.Commands
#region "IDisposable Members"
/// <summary>
/// Dispose Method.
/// Releases all resources used by the <see cref="CommandLineCmdletBase"/>.
/// </summary>
public void Dispose()
{
_process?.Dispose();
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases the unmanaged resources used by the <see cref="CommandLineCmdletBase"/>
/// and optionally releases the managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true"/> to release both managed and unmanaged resources;
/// <see langword="false"/> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_process?.Dispose();
}
}
#endregion "IDisposable Members"

View file

@ -39,9 +39,18 @@ namespace PSTests.Parallel
File.AppendAllText(testPath, testContent);
}
void IDisposable.Dispose()
public void Dispose()
{
File.Delete(testPath);
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
File.Delete(testPath);
}
}
private ExecutionContext GetExecutionContext()

View file

@ -97,18 +97,27 @@ namespace PSTests.Sequential
public void Dispose()
{
CleanupConfigFiles();
if (systemWideConfigBackupFile != null)
{
File.Move(systemWideConfigBackupFile, systemWideConfigFile);
}
Dispose(true);
GC.SuppressFinalize(this);
}
if (currentUserConfigBackupFile != null)
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
File.Move(currentUserConfigBackupFile, currentUserConfigFile);
}
CleanupConfigFiles();
if (systemWideConfigBackupFile != null)
{
File.Move(systemWideConfigBackupFile, systemWideConfigFile);
}
InternalTestHooks.BypassGroupPolicyCaching = originalTestHookValue;
if (currentUserConfigBackupFile != null)
{
File.Move(currentUserConfigBackupFile, currentUserConfigFile);
}
InternalTestHooks.BypassGroupPolicyCaching = originalTestHookValue;
}
}
internal PowerShellPolicies SystemWidePolicies