Skip to content

Commit 468f3ba

Browse files
committed
Selectively enable and fix a few more rules
1 parent 416b58a commit 468f3ba

File tree

18 files changed

+45
-30
lines changed

18 files changed

+45
-30
lines changed

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ dotnet_diagnostic.CA1506.severity = warning
5757
dotnet_diagnostic.CA1507.severity = error
5858
# CA1508: Avoid dead conditional code
5959
dotnet_diagnostic.CA1508.severity = error
60+
# CA1802: Use Literals Where Appropriate
61+
dotnet_diagnostic.CA1802.severity = error
62+
# CA1805: Do not initialize unnecessarily.
63+
dotnet_diagnostic.CA1805.severity = error
64+
# CA1820: Test for empty strings using string length
65+
dotnet_diagnostic.CA1820.severity = error
6066
# CA1822: Mark members as static
6167
dotnet_diagnostic.CA1822.severity = error
6268
# CA1823: Avoid unused private fields
@@ -65,9 +71,13 @@ dotnet_diagnostic.CA1823.severity = error
6571
dotnet_diagnostic.CA2007.severity = error
6672
# CA2016: Forward the CancellationToken parameter to methods that take one
6773
dotnet_diagnostic.CA2016.severity = error
74+
# CA2213: Disposable fields should be disposed
75+
dotnet_diagnostic.CA2213.severity = error
6876
# CA2254: The logging message template should not vary between calls to 'LoggerExtensions.*'
6977
dotnet_diagnostic.CA2254.severity = silent
7078

79+
# RCS1049: Simplify boolean comparison
80+
dotnet_diagnostic.RCS1049.severity = error
7181
# RCS1102: Make class static
7282
dotnet_diagnostic.RCS1102.severity = error
7383
# RCS1139: Add summary element to documentation comment

PowerShellEditorServices.Common.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<!-- See: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview -->
1515
<EnableNETAnalyzers>true</EnableNETAnalyzers>
1616
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
17+
<!-- TODO: Enable <AnalysisMode>All</AnalysisMode> -->
1718
<!-- See: https://docs.microsoft.com/en-us/dotnet/core/compatibility/sdk/6.0/implicit-namespaces -->
1819
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
1920
</PropertyGroup>

src/PowerShellEditorServices.Hosting/Configuration/EditorServicesConfig.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ public EditorServicesConfig(
7676
/// <summary>
7777
/// Names of or paths to any additional modules to load on startup.
7878
/// </summary>
79-
public IReadOnlyList<string> AdditionalModules { get; set; } = null;
79+
public IReadOnlyList<string> AdditionalModules { get; set; }
8080

8181
/// <summary>
8282
/// Flags of features to enable on startup.
8383
/// </summary>
84-
public IReadOnlyList<string> FeatureFlags { get; set; } = null;
84+
public IReadOnlyList<string> FeatureFlags { get; set; }
8585

8686
/// <summary>
8787
/// The console REPL experience to use in the integrated console
@@ -97,12 +97,12 @@ public EditorServicesConfig(
9797
/// <summary>
9898
/// Configuration for the language server protocol transport to use.
9999
/// </summary>
100-
public ITransportConfig LanguageServiceTransport { get; set; } = null;
100+
public ITransportConfig LanguageServiceTransport { get; set; }
101101

102102
/// <summary>
103103
/// Configuration for the debug adapter protocol transport to use.
104104
/// </summary>
105-
public ITransportConfig DebugServiceTransport { get; set; } = null;
105+
public ITransportConfig DebugServiceTransport { get; set; }
106106

107107
/// <summary>
108108
/// PowerShell profile locations for Editor Services to use for its profiles.

src/PowerShellEditorServices.Hosting/Configuration/TransportConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public sealed class StdioTransportConfig : ITransportConfig
4949

5050
public string SessionFileTransportName => "Stdio";
5151

52-
public IReadOnlyDictionary<string, object> SessionFileEntries { get; } = null;
52+
public IReadOnlyDictionary<string, object> SessionFileEntries { get; }
5353

5454
public Task<(Stream inStream, Stream outStream)> ConnectStreamsAsync()
5555
{

src/PowerShellEditorServices/Hosting/EditorServicesServerFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public static EditorServicesServerFactory Create(string logPath, int minimumLogL
5858
SelfLog.Enable(msg => Debug.WriteLine(msg));
5959
#endif
6060

61-
ILoggerFactory loggerFactory = new LoggerFactory().AddSerilog();
61+
LoggerFactory loggerFactory = new();
62+
loggerFactory.AddSerilog();
6263

6364
// Hook up logging from the host so that its recorded in the log file
6465
hostLogger.Subscribe(new HostLoggerAdapter(loggerFactory));

src/PowerShellEditorServices/Server/PsesDebugServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public void Dispose()
124124
_debugAdapterServer.Dispose();
125125
_inputStream.Dispose();
126126
_outputStream.Dispose();
127+
_loggerFactory.Dispose();
127128
_serverStopped.SetResult(true);
128129
// TODO: If the debugger has stopped, should we clear the breakpoints?
129130
}

src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public AnalysisService(
136136
public void StartScriptDiagnostics(
137137
ScriptFile[] filesToAnalyze)
138138
{
139-
if (_configurationService.CurrentSettings.ScriptAnalysis.Enable == false)
139+
if (!_configurationService.CurrentSettings.ScriptAnalysis.Enable)
140140
{
141141
return;
142142
}
@@ -468,7 +468,7 @@ private static Hashtable GetCommentHelpRuleSettings(string helpLocation, bool fo
468468
}
469469

470470
#region IDisposable Support
471-
private bool disposedValue = false; // To detect redundant calls
471+
private bool disposedValue; // To detect redundant calls
472472

473473
protected virtual void Dispose(bool disposing)
474474
{

src/PowerShellEditorServices/Services/Analysis/PssaCmdletAnalysisEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public Task<ScriptFileMarker[]> AnalyzeScriptAsync(string scriptContent, Hashtab
258258
public PssaCmdletAnalysisEngine RecreateWithRules(string[] rules) => new(_logger, _analysisRunspacePool, _pssaModuleInfo, rules);
259259

260260
#region IDisposable Support
261-
private bool disposedValue = false; // To detect redundant calls
261+
private bool disposedValue; // To detect redundant calls
262262

263263
protected virtual void Dispose(bool disposing)
264264
{

src/PowerShellEditorServices/Services/PowerShell/Utility/ErrorRecordExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility
1212
{
1313
internal static class ErrorRecordExtensions
1414
{
15-
private static readonly Action<PSObject> s_setWriteStreamProperty = null;
15+
private static readonly Action<PSObject> s_setWriteStreamProperty;
1616

1717
[SuppressMessage("Performance", "CA1810:Initialize reference type static fields inline", Justification = "cctor needed for version specific initialization")]
1818
static ErrorRecordExtensions()

src/PowerShellEditorServices/Services/TextDocument/FoldingReference.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal class FoldingReference : IComparable<FoldingReference>, IEquatable<Fold
2020
/// <summary>
2121
/// The zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line.
2222
/// </summary>
23-
public int StartCharacter { get; set; } = 0;
23+
public int StartCharacter { get; set; }
2424

2525
/// <summary>
2626
/// The zero-based line number where the folded range ends.
@@ -30,7 +30,7 @@ internal class FoldingReference : IComparable<FoldingReference>, IEquatable<Fold
3030
/// <summary>
3131
/// The zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line.
3232
/// </summary>
33-
public int EndCharacter { get; set; } = 0;
33+
public int EndCharacter { get; set; }
3434

3535
/// <summary>
3636
/// Describes the kind of the folding range such as `comment' or 'region'.

src/PowerShellEditorServices/Services/TextDocument/TokenOperations.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ namespace Microsoft.PowerShell.EditorServices.Services.TextDocument
1313
/// </summary>
1414
internal static class TokenOperations
1515
{
16-
private static readonly FoldingRangeKind? RegionKindNone = null;
17-
1816
// These regular expressions are used to match lines which mark the start and end of region comment in a PowerShell
1917
// script. They are based on the defaults in the VS Code Language Configuration at;
2018
// https://github.com/Microsoft/vscode/blob/64186b0a26/extensions/powershell/language-configuration.json#L26-L31
@@ -48,7 +46,7 @@ internal static FoldingReferenceList FoldableReferences(
4846
case TokenKind.RCurly:
4947
if (tokenCurlyStack.Count > 0)
5048
{
51-
refList.SafeAdd(CreateFoldingReference(tokenCurlyStack.Pop(), token, RegionKindNone));
49+
refList.SafeAdd(CreateFoldingReference(tokenCurlyStack.Pop(), token, default));
5250
}
5351
break;
5452

@@ -64,7 +62,7 @@ internal static FoldingReferenceList FoldableReferences(
6462
case TokenKind.RParen:
6563
if (tokenParenStack.Count > 0)
6664
{
67-
refList.SafeAdd(CreateFoldingReference(tokenParenStack.Pop(), token, RegionKindNone));
65+
refList.SafeAdd(CreateFoldingReference(tokenParenStack.Pop(), token, default));
6866
}
6967
break;
7068

@@ -76,7 +74,7 @@ internal static FoldingReferenceList FoldableReferences(
7674
case TokenKind.HereStringExpandable:
7775
if (token.Extent.StartLineNumber != token.Extent.EndLineNumber)
7876
{
79-
refList.SafeAdd(CreateFoldingReference(token, token, RegionKindNone));
77+
refList.SafeAdd(CreateFoldingReference(token, token, default));
8078
}
8179
break;
8280
}

src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public class PesterSettings
383383
/// <summary>
384384
/// Whether integration features specific to Pester v5 are enabled
385385
/// </summary>
386-
public bool UseLegacyCodeLens { get; set; } = false;
386+
public bool UseLegacyCodeLens { get; set; }
387387

388388
/// <summary>
389389
/// Update these settings from another settings object

test/PowerShellEditorServices.Test.E2E/Processes/StdioServerProcess.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public class StdioServerProcess : ServerProcess
2323
/// <summary>
2424
/// The current server process (if any).
2525
/// </summary>
26+
#pragma warning disable CA2213
2627
private Process _serverProcess;
28+
#pragma warning restore CA2213
2729

2830
/// <summary>
2931
/// Create a new <see cref="StdioServerProcess"/>.
@@ -55,7 +57,7 @@ protected override void Dispose(bool disposing)
5557
if (disposing)
5658
{
5759
Process serverProcess = Interlocked.Exchange(ref _serverProcess, null);
58-
if (serverProcess != null)
60+
if (serverProcess is not null)
5961
{
6062
if (!serverProcess.HasExited)
6163
{
@@ -65,6 +67,7 @@ protected override void Dispose(bool disposing)
6567
serverProcess.Dispose();
6668
}
6769
}
70+
base.Dispose(disposing);
6871
}
6972

7073
/// <summary>
@@ -126,7 +129,7 @@ public override Task Stop()
126129
return ServerExitCompletion.Task;
127130
}
128131

129-
public event EventHandler<ProcessExitedArgs> ProcessExited;
132+
public event EventHandler<ProcessExitedEventArgs> ProcessExited;
130133

131134
/// <summary>
132135
/// Called when the server process has exited.
@@ -147,7 +150,7 @@ private void ServerProcess_Exit(object sender, EventArgs args)
147150
string errorMsg = serverProcess.StandardError.ReadToEnd();
148151

149152
OnExited();
150-
ProcessExited?.Invoke(this, new ProcessExitedArgs(exitCode, errorMsg));
153+
ProcessExited?.Invoke(this, new ProcessExitedEventArgs(exitCode, errorMsg));
151154
if (exitCode != 0)
152155
{
153156
ServerExitCompletion.TrySetException(new ProcessExitedException("Stdio server process exited unexpectedly", exitCode, errorMsg));
@@ -174,9 +177,9 @@ public ProcessExitedException(string message, int exitCode, string errorMessage)
174177
public string ErrorMessage { get; init; }
175178
}
176179

177-
public class ProcessExitedArgs : EventArgs
180+
public class ProcessExitedEventArgs : EventArgs
178181
{
179-
public ProcessExitedArgs(int exitCode, string errorMessage)
182+
public ProcessExitedEventArgs(int exitCode, string errorMessage)
180183
{
181184
ExitCode = exitCode;
182185
ErrorMessage = errorMessage;

test/PowerShellEditorServices.Test.Shared/Completion/CompleteCommandFromModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Microsoft.PowerShell.EditorServices.Test.Shared.Completion
88
{
99
internal static class CompleteCommandFromModule
1010
{
11-
public static readonly string GetRandomDetail =
11+
public const string GetRandomDetail =
1212
"Get-Random [[-Maximum] <Object>] [-SetSeed <int>] [-Minimum <Object>]";
1313

1414
public static readonly ScriptRegion SourceDetails = new(

test/PowerShellEditorServices.Test.Shared/TestUtilities/TestUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static string NormalizeNewlines(string unixString)
8181
/// <summary>
8282
/// Not for use in production -- convenience code for debugging tests.
8383
/// </summary>
84-
public static void AWAIT_DEBUGGER_HERE(
84+
public static void AwaitDebuggerHere(
8585
[CallerMemberName] string callerName = null,
8686
[CallerFilePath] string callerPath = null,
8787
[CallerLineNumber] int callerLine = -1)

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public DebugServiceTests()
6262
public void Dispose()
6363
{
6464
debugService.Abort();
65+
debuggerStoppedQueue.Dispose();
6566
psesHost.StopAsync().Wait();
6667
GC.SuppressFinalize(this);
6768
}
@@ -113,7 +114,7 @@ private void AssertDebuggerStopped(
113114

114115
Assert.True(psesHost.DebugContext.IsStopped);
115116

116-
if (scriptPath != "")
117+
if (!string.IsNullOrEmpty(scriptPath))
117118
{
118119
// TODO: The drive letter becomes lower cased on Windows for some reason.
119120
Assert.Equal(scriptPath, eventArgs.ScriptPath, ignoreCase: true);

test/PowerShellEditorServices.Test/Session/PathEscapingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class PathEscapingTests
2323
[InlineData("/CJK.chars/脚本/[hello].ps1", "/CJK.chars/脚本/`[hello`].ps1")]
2424
[InlineData("C:\\Animals\\утка\\quack.ps1", "C:\\Animals\\утка\\quack.ps1")]
2525
[InlineData("C:\\&nimals\\утка\\qu*ck?.ps1", "C:\\&nimals\\утка\\qu`*ck`?.ps1")]
26-
public void CorrectlyWildcardEscapesPaths_NoSpaces(string unescapedPath, string escapedPath)
26+
public void CorrectlyWildcardEscapesPathsNoSpaces(string unescapedPath, string escapedPath)
2727
{
2828
string extensionEscapedPath = PathUtils.WildcardEscapePath(unescapedPath);
2929
Assert.Equal(escapedPath, extensionEscapedPath);
@@ -44,7 +44,7 @@ public void CorrectlyWildcardEscapesPaths_NoSpaces(string unescapedPath, string
4444
[InlineData("/CJK chars/脚本/[hello].ps1", "/CJK` chars/脚本/`[hello`].ps1")]
4545
[InlineData("C:\\Animal s\\утка\\quack.ps1", "C:\\Animal` s\\утка\\quack.ps1")]
4646
[InlineData("C:\\&nimals\\утка\\qu*ck?.ps1", "C:\\&nimals\\утка\\qu`*ck`?.ps1")]
47-
public void CorrectlyWildcardEscapesPaths_Spaces(string unescapedPath, string escapedPath)
47+
public void CorrectlyWildcardEscapesPathsSpaces(string unescapedPath, string escapedPath)
4848
{
4949
string extensionEscapedPath = PathUtils.WildcardEscapePath(unescapedPath, escapeSpaces: true);
5050
Assert.Equal(escapedPath, extensionEscapedPath);

test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,11 @@ public void CanGetRangeAtLineBoundaries()
377377

378378
[Trait("Category", "ScriptFile")]
379379
[Fact]
380-
public void CanSplitLines_NoTrailingNewline() => Assert.Equal(s_testStringLines_noTrailingNewline, _scriptFile_noTrailingNewline.FileLines);
380+
public void CanSplitLinesNoTrailingNewline() => Assert.Equal(s_testStringLines_noTrailingNewline, _scriptFile_noTrailingNewline.FileLines);
381381

382382
[Trait("Category", "ScriptFile")]
383383
[Fact]
384-
public void CanSplitLines_TrailingNewline() => Assert.Equal(s_testStringLines_trailingNewline, _scriptFile_trailingNewline.FileLines);
384+
public void CanSplitLinesTrailingNewline() => Assert.Equal(s_testStringLines_trailingNewline, _scriptFile_trailingNewline.FileLines);
385385

386386
[Trait("Category", "ScriptFile")]
387387
[Fact]

0 commit comments

Comments
 (0)