Skip to content

Commit 358db74

Browse files
authored
Enable CA1816: Dispose methods should call SuppressFinalize (#14074)
https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1816
1 parent cb6ef9b commit 358db74

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

.globalconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ dotnet_diagnostic.CA1814.severity = none
268268
dotnet_diagnostic.CA1815.severity = none
269269

270270
# CA1816: Dispose methods should call SuppressFinalize
271-
dotnet_diagnostic.CA1816.severity = suggestion
271+
dotnet_diagnostic.CA1816.severity = warning
272272

273273
# CA1819: Properties should not return arrays
274274
dotnet_diagnostic.CA1819.severity = none

src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,28 @@ public class CommandLineCmdletBase : PSCmdlet, IDisposable
101101
#region "IDisposable Members"
102102

103103
/// <summary>
104-
/// Dispose Method.
104+
/// Releases all resources used by the <see cref="CommandLineCmdletBase"/>.
105105
/// </summary>
106106
public void Dispose()
107107
{
108-
_process?.Dispose();
108+
Dispose(true);
109+
GC.SuppressFinalize(this);
110+
}
111+
112+
/// <summary>
113+
/// Releases the unmanaged resources used by the <see cref="CommandLineCmdletBase"/>
114+
/// and optionally releases the managed resources.
115+
/// </summary>
116+
/// <param name="disposing">
117+
/// <see langword="true"/> to release both managed and unmanaged resources;
118+
/// <see langword="false"/> to release only unmanaged resources.
119+
/// </param>
120+
protected virtual void Dispose(bool disposing)
121+
{
122+
if (disposing)
123+
{
124+
_process?.Dispose();
125+
}
109126
}
110127

111128
#endregion "IDisposable Members"

test/xUnit/csharp/test_FileSystemProvider.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,18 @@ public FileSystemProviderTests()
3939
File.AppendAllText(testPath, testContent);
4040
}
4141

42-
void IDisposable.Dispose()
42+
public void Dispose()
4343
{
44-
File.Delete(testPath);
44+
Dispose(true);
45+
GC.SuppressFinalize(this);
46+
}
47+
48+
protected virtual void Dispose(bool disposing)
49+
{
50+
if (disposing)
51+
{
52+
File.Delete(testPath);
53+
}
4554
}
4655

4756
private ExecutionContext GetExecutionContext()

test/xUnit/csharp/test_PSConfiguration.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,27 @@ public PowerShellPolicyFixture()
9797

9898
public void Dispose()
9999
{
100-
CleanupConfigFiles();
101-
if (systemWideConfigBackupFile != null)
102-
{
103-
File.Move(systemWideConfigBackupFile, systemWideConfigFile);
104-
}
100+
Dispose(true);
101+
GC.SuppressFinalize(this);
102+
}
105103

106-
if (currentUserConfigBackupFile != null)
104+
protected virtual void Dispose(bool disposing)
105+
{
106+
if (disposing)
107107
{
108-
File.Move(currentUserConfigBackupFile, currentUserConfigFile);
109-
}
108+
CleanupConfigFiles();
109+
if (systemWideConfigBackupFile != null)
110+
{
111+
File.Move(systemWideConfigBackupFile, systemWideConfigFile);
112+
}
110113

111-
InternalTestHooks.BypassGroupPolicyCaching = originalTestHookValue;
114+
if (currentUserConfigBackupFile != null)
115+
{
116+
File.Move(currentUserConfigBackupFile, currentUserConfigFile);
117+
}
118+
119+
InternalTestHooks.BypassGroupPolicyCaching = originalTestHookValue;
120+
}
112121
}
113122

114123
internal PowerShellPolicies SystemWidePolicies

0 commit comments

Comments
 (0)