Skip to content

Commit a83cd6c

Browse files
committed
Be consistent with error suppression
Avoiding pragmas wherever possible.
1 parent 9b6a2eb commit a83cd6c

22 files changed

+47
-47
lines changed

.editorconfig

+9-9
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ dotnet_diagnostic.CA1068.severity = error
5454
# CA1501: Avoid excessive inheritance
5555
dotnet_diagnostic.CA1501.severity = error
5656
# CA1502: Avoid excessive complexity
57-
dotnet_diagnostic.CA1502.severity = warning
57+
dotnet_diagnostic.CA1502.severity = silent
5858
# CA1505: Avoid unmaintainable code
5959
dotnet_diagnostic.CA1505.severity = error
6060
# CA1506: Avoid excessive class coupling
61-
dotnet_diagnostic.CA1506.severity = warning
61+
dotnet_diagnostic.CA1506.severity = silent
6262
# CA1507: Use nameof in place of string
6363
dotnet_diagnostic.CA1507.severity = error
6464
# CA1508: Avoid dead conditional code
@@ -95,22 +95,22 @@ dotnet_diagnostic.RCS1210.severity = error
9595
# RCS1036: Remove unnecessary blank line
9696
dotnet_diagnostic.RCS1036.severity = error
9797
# RCS1075: Avoid empty catch clause that catches System.Exception
98-
dotnet_diagnostic.RCS1075.severity = suggestion
98+
dotnet_diagnostic.RCS1075.severity = error
9999
# RCS1170: Use read-only auto-implemented property
100100
dotnet_diagnostic.RCS1170.severity = error
101101

102102
# VSTHRD002: Avoid problematic synchronous waits
103-
dotnet_diagnostic.VSTHRD002.severity = suggestion
103+
dotnet_diagnostic.VSTHRD002.severity = error
104104
# VSTHRD003: Avoid awaiting foreign Tasks
105-
dotnet_diagnostic.VSTHRD003.severity = suggestion
105+
dotnet_diagnostic.VSTHRD003.severity = error
106106
# VSTHRD105: Avoid method overloads that assume TaskScheduler.Current
107-
dotnet_diagnostic.VSTHRD105.severity = suggestion
107+
dotnet_diagnostic.VSTHRD105.severity = error
108108
# VSTHRD100: Avoid async void methods
109-
dotnet_diagnostic.VSTHRD100.severity = suggestion
109+
dotnet_diagnostic.VSTHRD100.severity = error
110110
# VSTHRD103: Call async methods when in an async method
111-
dotnet_diagnostic.VSTHRD103.severity = suggestion
111+
dotnet_diagnostic.VSTHRD103.severity = error
112112
# VSTHRD110: Observe result of async calls
113-
dotnet_diagnostic.VSTHRD110.severity = suggestion
113+
dotnet_diagnostic.VSTHRD110.severity = error
114114
# VSTHRD114: Avoid returning a null Task
115115
dotnet_diagnostic.VSTHRD114.severity = error
116116
# VSTHRD200: Use "Async" suffix for awaitable methods

src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ protected override void BeginProcessing()
215215
}
216216
#pragma warning restore IDE0022
217217

218-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Uses ThrowTerminatingError() instead")]
218+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "We have to wait here, it's the whole program.")]
219219
protected override void EndProcessing()
220220
{
221221
_logger.Log(PsesLogLevel.Diagnostic, "Beginning EndProcessing block");
@@ -232,9 +232,7 @@ protected override void EndProcessing()
232232
using EditorServicesLoader psesLoader = EditorServicesLoader.Create(_logger, editorServicesConfig, SessionDetailsPath, _loggerUnsubscribers);
233233
_logger.Log(PsesLogLevel.Verbose, "Loading EditorServices");
234234
// Synchronously start editor services and wait here until it shuts down.
235-
#pragma warning disable VSTHRD002
236235
psesLoader.LoadAndRunEditorServicesAsync().GetAwaiter().GetResult();
237-
#pragma warning restore VSTHRD002
238236
}
239237
catch (Exception e)
240238
{

src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -382,19 +382,18 @@ private void ValidateConfiguration()
382382
}
383383
}
384384

385+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1825:Avoid zero-length array allocations", Justification = "Cannot use Array.Empty, since it must work in net452")]
385386
private static Version GetPSVersion()
386387
{
387388
// In order to read the $PSVersionTable variable,
388389
// we are forced to create a new runspace to avoid concurrency issues,
389390
// which is expensive.
390391
// Rather than do that, we instead go straight to the source,
391392
// which is a static property, internal in WinPS and public in PS 6+
392-
#pragma warning disable CA1825
393393
return typeof(PSObject).Assembly
394394
.GetType("System.Management.Automation.PSVersionInfo")
395395
.GetMethod("get_PSVersion", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
396-
.Invoke(null, new object[0] /* Cannot use Array.Empty, since it must work in net452 */) as Version;
397-
#pragma warning restore CA1825
396+
.Invoke(null, new object[0]) as Version;
398397
}
399398
}
400399
}

src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs

+2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ private async Task RunTempDebugSessionAsync(HostStartupInfo hostDetails)
201201
await debugServer.WaitForShutdown().ConfigureAwait(false);
202202
}
203203

204+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD003:Avoid awaiting foreign Tasks", Justification = "It's a wrapper.")]
204205
private async Task StartDebugServer(Task<PsesDebugServer> debugServerCreation)
205206
{
206207
PsesDebugServer debugServer = await debugServerCreation.ConfigureAwait(false);
@@ -304,6 +305,7 @@ private void WriteStartupBanner()
304305
_config.PSHost.UI.WriteLine(_config.StartupBanner);
305306
}
306307

308+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD110:Observe result of async calls", Justification = "Intentionally fire and forget.")]
307309
private void DebugServer_OnSessionEnded(object sender, EventArgs args)
308310
{
309311
_logger.Log(PsesLogLevel.Verbose, "Debug session ended, restarting debug service...");

src/PowerShellEditorServices/Extensions/EditorContext.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ public void SetSelection(
9292
/// Sets a selection in the host editor's active buffer.
9393
/// </summary>
9494
/// <param name="selectionRange">The range of the selection.</param>
95-
#pragma warning disable VSTHRD002
95+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
9696
public void SetSelection(FileRange selectionRange) => editorOperations.SetSelectionAsync(selectionRange.ToBufferRange()).Wait();
97-
#pragma warning restore VSTHRD002
9897

9998
#endregion
10099
}

src/PowerShellEditorServices/Extensions/EditorObject.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,12 @@ internal EditorObject(
115115
/// at the time this method is invoked.
116116
/// </summary>
117117
/// <returns>A instance of the EditorContext class.</returns>
118-
#pragma warning disable VSTHRD002, VSTHRD104
118+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
119119
public EditorContext GetEditorContext() => _editorOperations.GetEditorContextAsync().Result;
120-
#pragma warning restore VSTHRD002, VSTHRD104
121120

122121
internal void SetAsStaticInstance()
123122
{
124-
EditorObject.Instance = this;
123+
Instance = this;
125124
s_editorObjectReady.TrySetResult(true);
126125
}
127126
}

src/PowerShellEditorServices/Extensions/EditorWindow.cs

+5
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,36 @@ internal EditorWindow(IEditorOperations editorOperations)
4343
/// Shows an informational message to the user.
4444
/// </summary>
4545
/// <param name="message">The message to be shown.</param>
46+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
4647
public void ShowInformationMessage(string message) => editorOperations.ShowInformationMessageAsync(message).Wait();
4748

4849
/// <summary>
4950
/// Shows an error message to the user.
5051
/// </summary>
5152
/// <param name="message">The message to be shown.</param>
53+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
5254
public void ShowErrorMessage(string message) => editorOperations.ShowErrorMessageAsync(message).Wait();
5355

5456
/// <summary>
5557
/// Shows a warning message to the user.
5658
/// </summary>
5759
/// <param name="message">The message to be shown.</param>
60+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
5861
public void ShowWarningMessage(string message) => editorOperations.ShowWarningMessageAsync(message).Wait();
5962

6063
/// <summary>
6164
/// Sets the status bar message in the editor UI (if applicable).
6265
/// </summary>
6366
/// <param name="message">The message to be shown.</param>
67+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
6468
public void SetStatusBarMessage(string message) => editorOperations.SetStatusBarMessageAsync(message, null).Wait();
6569

6670
/// <summary>
6771
/// Sets the status bar message in the editor UI (if applicable).
6872
/// </summary>
6973
/// <param name="message">The message to be shown.</param>
7074
/// <param name="timeout">A timeout in milliseconds for how long the message should remain visible.</param>
75+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
7176
public void SetStatusBarMessage(string message, int timeout) => editorOperations.SetStatusBarMessageAsync(message, timeout).Wait();
7277

7378
#endregion

src/PowerShellEditorServices/Extensions/EditorWorkspace.cs

+7
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,22 @@ public sealed class EditorWorkspace
4242
/// <summary>
4343
/// Creates a new file in the editor.
4444
/// </summary>
45+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
4546
public void NewFile() => editorOperations.NewFileAsync(string.Empty).Wait();
4647

4748
/// <summary>
4849
/// Creates a new file in the editor.
4950
/// </summary>
5051
/// <param name="content">The content to place in the new file.</param>
52+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
5153
public void NewFile(string content) => editorOperations.NewFileAsync(content).Wait();
5254

5355
/// <summary>
5456
/// Opens a file in the workspace. If the file is already open
5557
/// its buffer will be made active.
5658
/// </summary>
5759
/// <param name="filePath">The path to the file to be opened.</param>
60+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
5861
public void OpenFile(string filePath) => editorOperations.OpenFileAsync(filePath).Wait();
5962

6063
/// <summary>
@@ -64,25 +67,29 @@ public sealed class EditorWorkspace
6467
/// </summary>
6568
/// <param name="filePath">The path to the file to be opened.</param>
6669
/// <param name="preview">Determines wether the file is opened as a preview or as a durable editor.</param>
70+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
6771
public void OpenFile(string filePath, bool preview) => editorOperations.OpenFileAsync(filePath, preview).Wait();
6872

6973
/// <summary>
7074
/// Closes a file in the workspace.
7175
/// </summary>
7276
/// <param name="filePath">The path to the file to be closed.</param>
77+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
7378
public void CloseFile(string filePath) => editorOperations.CloseFileAsync(filePath).Wait();
7479

7580
/// <summary>
7681
/// Saves an open file in the workspace.
7782
/// </summary>
7883
/// <param name="filePath">The path to the file to be saved.</param>
84+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
7985
public void SaveFile(string filePath) => editorOperations.SaveFileAsync(filePath).Wait();
8086

8187
/// <summary>
8288
/// Saves a file with a new name AKA a copy.
8389
/// </summary>
8490
/// <param name="oldFilePath">The file to copy.</param>
8591
/// <param name="newFilePath">The file to create.</param>
92+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
8693
public void SaveFile(string oldFilePath, string newFilePath) => editorOperations.SaveFileAsync(oldFilePath, newFilePath).Wait();
8794

8895
#endregion

src/PowerShellEditorServices/Extensions/FileContext.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,13 @@ public void InsertText(
208208
/// </summary>
209209
/// <param name="textToInsert">The text string to insert.</param>
210210
/// <param name="insertRange">The buffer range which will be replaced by the string.</param>
211-
#pragma warning disable VSTHRD002
211+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
212212
public void InsertText(string textToInsert, IFileRange insertRange)
213213
{
214214
editorOperations
215215
.InsertTextAsync(scriptFile.DocumentUri.ToString(), textToInsert, insertRange.ToBufferRange())
216216
.Wait();
217217
}
218-
#pragma warning restore VSTHRD002
219218

220219
#endregion
221220

@@ -224,6 +223,7 @@ public void InsertText(string textToInsert, IFileRange insertRange)
224223
/// <summary>
225224
/// Saves this file.
226225
/// </summary>
226+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD110:Observe result of async calls", Justification = "Supporting synchronous API.")]
227227
public void Save() => editorOperations.SaveFileAsync(scriptFile.FilePath);
228228

229229
/// <summary>
@@ -233,7 +233,7 @@ public void InsertText(string textToInsert, IFileRange insertRange)
233233
/// the path where the file should be saved,
234234
/// including the file name with extension as the leaf
235235
/// </param>
236-
#pragma warning disable VSTHRD002
236+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "Supporting synchronous API.")]
237237
public void SaveAs(string newFilePath)
238238
{
239239
// Do some validation here so that we can provide a helpful error if the path won't work
@@ -248,7 +248,6 @@ public void SaveAs(string newFilePath)
248248

249249
editorOperations.SaveFileAsync(scriptFile.FilePath, newFilePath).Wait();
250250
}
251-
#pragma warning restore VSTHRD002
252251

253252
#endregion
254253
}

src/PowerShellEditorServices/GlobalSuppressions.cs

-9
This file was deleted.

src/PowerShellEditorServices/IsExternalInit.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#pragma warning disable IDE0073
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
24
#if NET5_0_OR_GREATER
35
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.CompilerServices.IsExternalInit))]
46
#else

src/PowerShellEditorServices/Server/PsesLanguageServer.cs

-2
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ public PsesLanguageServer(
7070
/// cref="PsesServiceCollectionExtensions.AddPsesLanguageServices"/>.
7171
/// </remarks>
7272
/// <returns>A task that completes when the server is ready and listening.</returns>
73-
#pragma warning disable CA1506 // Coupling complexity we don't care about
7473
public async Task StartAsync()
75-
#pragma warning restore CA1506
7674
{
7775
LanguageServer = await OmniSharp.Extensions.LanguageServer.Server.LanguageServer.From(options =>
7876
{

src/PowerShellEditorServices/Server/PsesServiceCollectionExtensions.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Microsoft.PowerShell.EditorServices.Server
1717
{
1818
internal static class PsesServiceCollectionExtensions
1919
{
20+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD110:Observe result of async calls", Justification = "Using lazy initialization.")]
2021
public static IServiceCollection AddPsesLanguageServices(
2122
this IServiceCollection collection,
2223
HostStartupInfo hostStartupInfo)
@@ -48,9 +49,7 @@ public static IServiceCollection AddPsesLanguageServices(
4849
// is ready, it will be available. NOTE: We cannot await this because it
4950
// uses a lazy initialization to avoid a race with the dependency injection
5051
// framework, see the EditorObject class for that!
51-
#pragma warning disable VSTHRD110
5252
extensionService.InitializeAsync();
53-
#pragma warning restore VSTHRD110
5453
return extensionService;
5554
})
5655
.AddSingleton<AnalysisService>();

src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ private static string TrimScriptListingLine(PSObject scriptLineObj, ref int pref
923923
/// </summary>
924924
public event EventHandler<DebuggerStoppedEventArgs> DebuggerStopped;
925925

926+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "It has to be async.")]
926927
internal async void OnDebuggerStopAsync(object sender, DebuggerStopEventArgs e)
927928
{
928929
try

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs

+1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ public async Task OnStarted(IDebugAdapterServer server, CancellationToken cancel
440440
// be sent to the client.
441441
await _debugStateService.ServerStarted.Task.ConfigureAwait(false);
442442

443+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD003:Avoid awaiting foreign Tasks", Justification = "It's a wrapper.")]
443444
private async Task OnExecutionCompletedAsync(Task executeTask)
444445
{
445446
bool isRunspaceClosed = false;

src/PowerShellEditorServices/Services/PowerShell/Console/LegacyReadLine.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ public LegacyReadLine(
3535
_onIdleAction = onIdleAction;
3636
}
3737

38-
#pragma warning disable CA1502 // Cyclomatic complexity we don't care about
3938
public override string ReadLine(CancellationToken cancellationToken)
40-
#pragma warning restore CA1502
4139
{
4240
string inputBeforeCompletion = null;
4341
string inputAfterCompletion = null;
@@ -394,6 +392,7 @@ protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken)
394392
}
395393
}
396394

395+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "This is a legacy implementation.")]
397396
private ConsoleKeyInfo ReadKeyWithIdleSupport(CancellationToken cancellationToken)
398397
{
399398
// We run the readkey function on another thread so we can run an idle handler

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
2929
using Microsoft.PowerShell.EditorServices.Server;
3030
using OmniSharp.Extensions.DebugAdapter.Protocol.Server;
3131

32-
#pragma warning disable CA1506 // Coupling complexity we don't care about
3332
internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRunspaceContext, IInternalPowerShellExecutionService
34-
#pragma warning restore CA1506
3533
{
3634
internal const string DefaultPrompt = "> ";
3735

src/PowerShellEditorServices/Services/TextDocument/Handlers/DidChangeWatchedFilesHandler.cs

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public DidChangeWatchedFilesHandler(
4141
public DidChangeWatchedFilesRegistrationOptions GetRegistrationOptions(
4242
DidChangeWatchedFilesCapability capability,
4343
ClientCapabilities clientCapabilities)
44+
#pragma warning disable CS8601 // Possible null reference assignment (it's from the library).
4445
=> new()
4546
{
4647
Watchers = new[]
@@ -52,6 +53,7 @@ public DidChangeWatchedFilesRegistrationOptions GetRegistrationOptions(
5253
},
5354
},
5455
};
56+
#pragma warning restore CS8601 // Possible null reference assignment.
5557

5658
public Task<Unit> Handle(DidChangeWatchedFilesParams request, CancellationToken cancellationToken)
5759
{

0 commit comments

Comments
 (0)