Skip to content

Commit 508fc87

Browse files
authored
Fix analyzer errors in Renci.SshNet and Renci.SshNet.TestTools.OpenSSH (#1229)
* Fix analyzer errors in Renci.SshNet and Renci.SshNet.TestTools.OpenSSH. Suppress all errors in unit tests and integration tests. * Update unit tests now that we pass 'mode' as argument name when we throw ArgumentException. * Remove stale comment and add unit tests for SshData.ReadBytes(int length). * Remove unnessary suppression. * Remove Visual Studio magic. * Removed duplicate source file. * Clarified that suppression hides a false positive. * Remove suppressions for S2372. * Update ReadExtensionPair() to return concrete dictionary.
1 parent f745b3f commit 508fc87

File tree

217 files changed

+4231
-2099
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+4231
-2099
lines changed

.editorconfig

+50
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ dotnet_diagnostic.S1172.severity = none
7777
# This is a duplicate of IDE0059.
7878
dotnet_diagnostic.S1481.severity = none
7979

80+
# S1854: Unused assignments should be removed
81+
# https://rules.sonarsource.com/csharp/RSPEC-1854
82+
#
83+
# This is a duplicate of IDE0059.
84+
dotnet_diagnostic.S1854.severity = none
85+
8086
# S2259: Null pointers should not be dereferenced
8187
# https://rules.sonarsource.com/csharp/RSPEC-2259
8288
#
@@ -168,6 +174,12 @@ dotnet_diagnostic.S3928.severity = none
168174
# This is a duplicate of CA2002, and partial duplicate of MA0064.
169175
dotnet_diagnostic.S3998.severity = none
170176

177+
# S4070: Non-flags enums should not be marked with "FlagsAttribute"
178+
# https://rules.sonarsource.com/csharp/RSPEC-4070
179+
#
180+
# This is a duplicate of MA0062.
181+
dotnet_diagnostic.S4070.severity = none
182+
171183
# S4456: Parameter validation in yielding methods should be wrapped
172184
# https://rules.sonarsource.com/csharp/RSPEC-4456
173185
#
@@ -364,6 +376,14 @@ dotnet_diagnostic.MA0018.severity = none
364376
# No strong need for this, and may negatively affect performance.
365377
dotnet_diagnostic.MA0021.severity = none
366378

379+
# MA0025: Implement the functionality instead of throwing NotImplementedException
380+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0031.md
381+
dotnet_diagnostic.MA0025.severity = none
382+
383+
# MA0026: Fix TODO comment
384+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0026.md
385+
dotnet_diagnostic.MA0026.severity = suggestion
386+
367387
# MA0031: Optimize Enumerable.Count() usage
368388
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0031.md
369389
#
@@ -436,6 +456,12 @@ dotnet_diagnostic.MA0112.severity = error
436456
# instead of a base class or interface.
437457
dotnet_diagnostic.CA1002.severity = none
438458

459+
# CA1003: Use generic event handler instances
460+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1003
461+
#
462+
# Similar to MA0046.
463+
dotnet_diagnostic.CA1003.severity = none
464+
439465
# CA1008: Enums should have zero value
440466
#
441467
# TODO: To be discussed. Having a zero value offers a performance advantage.
@@ -460,6 +486,10 @@ dotnet_diagnostic.CA1051.severity = none
460486
# By default, this diagnostic is only reported for public types.
461487
dotnet_code_quality.CA1052.api_surface = all
462488

489+
# CA1065: Do not raise exceptions in unexpected locations
490+
# https://learn.microsoft.com/en-US/dotnet/fundamentals/code-analysis/quality-rules/ca1065
491+
dotnet_diagnostic.CA1065.severity = none
492+
463493
# CA1303: Do not pass literals as localized parameters
464494
#
465495
# We don't care about localization.
@@ -473,6 +503,10 @@ dotnet_diagnostic.CA1303.severity = none
473503
# Submitted https://github.com/dotnet/roslyn-analyzers/issues/6096 to fix CA1305.
474504
dotnet_diagnostic.CA1305.severity = none
475505

506+
# CA1309: Use ordinal StringComparison
507+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1309
508+
dotnet_diagnostic.CA1309.severity = none
509+
476510
# CA1510: Use ArgumentNullException throw helper
477511
#
478512
# This is only available in .NET 6.0 and higher. We'd need to use conditional compilation to only
@@ -577,6 +611,22 @@ dotnet_diagnostic.IDE0130.severity = none
577611
# var inputPath = originalDossierPathList.Find(x => x.id == updatedPath.id) ?? throw new PcsException($"Path id ({updatedPath.id}) unknown in PCS for dossier id {dossierFromTs.dossier.id}", updatedPath.id);
578612
dotnet_diagnostic.IDE0270.severity = none
579613

614+
# IDE0290: Use primary constructor
615+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0290
616+
dotnet_diagnostic.IDE0290.severity = none
617+
618+
# IDE0300: Collection initialization can be simplified
619+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0300
620+
#
621+
# TODO: Discuss whether we want to start using this
622+
dotnet_diagnostic.IDE0300.severity = none
623+
624+
# IDE0301: Simplify collection initialization
625+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0301
626+
#
627+
# TODO: Discuss whether we want to start using this
628+
dotnet_diagnostic.IDE0301.severity = none
629+
580630
#### .NET Compiler Platform code style rules ####
581631

582632
### Language rules ###

Directory.Build.props

+3-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
Code analysis properties.
1919
-->
2020
<PropertyGroup>
21-
<EnableNETAnalyzers>false</EnableNETAnalyzers>
21+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
2222
<AnalysisLevel>preview-All</AnalysisLevel>
2323
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
2424
</PropertyGroup>
@@ -34,11 +34,9 @@
3434
Use fixed version of analyzers.
3535
-->
3636
<ItemGroup>
37-
<!--
3837
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0-preview1.23165.1" PrivateAssets="all" />
3938
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="all" />
40-
<PackageReference Include="Meziantou.Analyzer" Version="2.0.54" PrivateAssets="all" />
41-
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.55.0.65544" PrivateAssets="all" />
42-
-->
39+
<PackageReference Include="Meziantou.Analyzer" Version="2.0.103" PrivateAssets="all" />
40+
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.12.0.78982" PrivateAssets="all" />
4341
</ItemGroup>
4442
</Project>

src/Renci.SshNet/.editorconfig

+121-2
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,161 @@
11
[*.cs]
22

3+
#### Sonar rules ####
4+
5+
# S1264: A "while" loop should be used instead of a "for" loop
6+
# https://rules.sonarsource.com/csharp/RSPEC-1264
7+
dotnet_diagnostic.S1264.severity = none
8+
9+
# S1450: Private fields only used as local variables in methods should become local variables
10+
# https://rules.sonarsource.com/csharp/RSPEC-1450
11+
#
12+
# TODO: Re-enable when the following issue is resolved:
13+
# https://github.com/SonarSource/sonar-dotnet/issues/8239
14+
dotnet_diagnostic.S1450.severity = none
15+
16+
# S2372: Exceptions should not be thrown from property getters
17+
# https://rules.sonarsource.com/csharp/RSPEC-2372/
18+
dotnet_diagnostic.S2372.severity = none
19+
20+
# S2583: Conditionally executed code should be reachable
21+
# https://rules.sonarsource.com/csharp/RSPEC-2583/
22+
#
23+
# TODO: Re-enable when the following issue is resolved:
24+
# https://github.com/SonarSource/sonar-dotnet/issues/8264
25+
dotnet_diagnostic.S2583.severity = none
26+
27+
# S2589: Boolean expressions should not be gratuitous
28+
# https://rules.sonarsource.com/csharp/RSPEC-2589/
29+
#
30+
# TODO: Re-enable when the following issue is resolved:
31+
# https://github.com/SonarSource/sonar-dotnet/issues/8262
32+
dotnet_diagnostic.S2589.severity = none
33+
34+
dotnet_diagnostic.S2372.severity = none
35+
336
#### SYSLIB diagnostics ####
437

538
# SYSLIB1045: Use 'GeneratedRegexAttribute' to generate the regular expression implementation at compile-time
639
#
740
# TODO: Remove this when https://github.com/sshnet/SSH.NET/issues/1131 is implemented.
841
dotnet_diagnostic.SYSLIB1045.severity = none
942

10-
### StyleCop Analyzers rules ###
43+
#### StyleCop Analyzers rules ####
44+
45+
# SA1123: Do not place regions within elements
46+
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1123.md
47+
dotnet_diagnostic.SA1123.severity = none
48+
49+
# SA1124: Do not use regions
50+
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1124.md
51+
dotnet_diagnostic.SA1124.severity = none
1152

1253
# SA1202: Elements must be ordered by access
1354
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md
1455
dotnet_diagnostic.SA1202.severity = none
1556

57+
# SA1204: Static elements must appear before instance elements
58+
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1204.md
59+
dotnet_diagnostic.SA1204.severity = none
60+
61+
# SA1310: Field names must not contain underscore
62+
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1310.md
63+
#dotnet_diagnostic.SA1310.severity = none
64+
65+
# SA1312: Variable names should begin with lower-case letter
66+
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1312.md
67+
dotnet_diagnostic.SA1312.severity = none
68+
69+
# SA1636: File header copyright text should match
70+
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1636.md
71+
dotnet_diagnostic.SA1636.severity = none
72+
73+
# SA1643: Destructor summary documentation must begin with standard text
74+
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1643.md
75+
dotnet_diagnostic.SA1643.severity = none
76+
1677
#### Meziantou.Analyzer rules ####
1778

79+
# MA0001: StringComparison is missing
80+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0001.md
81+
dotnet_diagnostic.MA0001.severity = none
82+
83+
# MA0011: IFormatProvider is missing
84+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0011.md
85+
#
86+
# TODO: Remove exclusion when issues are fixed
87+
dotnet_diagnostic.MA0011.severity = none
88+
89+
# MA0015: Specify the parameter name in ArgumentException
90+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0015.md
91+
#
92+
# TODO: Remove exclusion when issues are fixed
93+
dotnet_diagnostic.MA0015.severity = none
94+
95+
# MA0050: Validate arguments correctly in iterator methods
96+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0050.md
97+
#
98+
# TODO: Re-enable when https://github.com/meziantou/Meziantou.Analyzer/issues/617 is fixed
99+
dotnet_diagnostic.MA0050.severity = none
100+
18101
# MA0053: Make class sealed
19102
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0053.md
20103
MA0053.public_class_should_be_sealed = false
21104

105+
# MA0055: Do not use finalizer
106+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0055.md
107+
#
108+
# TODO: Remove exclusion when issues are fixed
109+
dotnet_diagnostic.MA0055.severity = none
110+
111+
# MA0110: Use the Regex source generator
112+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0110.md
113+
dotnet_diagnostic.MA0110.severity = none
114+
22115
#### .NET Compiler Platform analysers rules ####
23116

24117
# CA1030: Use events where appropriate
25118
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1030
26-
dotnet_diagnostic.CA10310.severity = none
119+
dotnet_diagnostic.CA1030.severity = none
27120

28121
# CA1031: Do not catch general exception types
29122
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1031
30123
dotnet_diagnostic.CA1031.severity = none
31124

125+
# CA1062: Validate arguments of public methods
126+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1062
127+
#
128+
# TODO: Remove exclusion when issues are fixed
129+
dotnet_diagnostic.CA1062.severity = none
130+
131+
# CA1307: Specify StringComparison for clarity
132+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1307
133+
dotnet_diagnostic.CA1307.severity = none
134+
135+
# CA1716: Identifiers should not match keywords
136+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1716
137+
dotnet_diagnostic.CA1716.severity = none
138+
139+
# CA1822: Mark members as static
140+
# https://learn.microsoft.com/en-US/dotnet/fundamentals/code-analysis/quality-rules/ca1822
141+
dotnet_code_quality.CA1822.api_surface = private,internal
142+
32143
# CA2213: Disposable fields should be disposed
33144
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2213
34145
dotnet_diagnostic.CA2213.severity = none
35146

147+
# CA3075: Insecure DTD Processing
148+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca3075
149+
dotnet_diagnostic.CA3075.severity = none
150+
36151
# IDE0004: Types that own disposable fields should be disposable
37152
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0004
38153
dotnet_diagnostic.IDE0004.severity = none
39154

40155
# IDE0048: Add parentheses for clarity
41156
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0047
42157
dotnet_diagnostic.IDE0048.severity = none
158+
159+
# IDE0305: Collection initialization can be simplified
160+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0305
161+
dotnet_diagnostic.IDE0305.severity = none

src/Renci.SshNet/Abstractions/DiagnosticAbstraction.cs

-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ public static bool IsEnabled(TraceEventType traceEventType)
2222
public static void Log(string text)
2323
{
2424
Loggging.TraceEvent(TraceEventType.Verbose,
25-
#if NET6_0_OR_GREATER
2625
System.Environment.CurrentManagedThreadId,
27-
#else
28-
System.Threading.Thread.CurrentThread.ManagedThreadId,
29-
#endif // NET6_0_OR_GREATER
3026
text);
3127
}
3228
}

src/Renci.SshNet/Abstractions/DnsAbstraction.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal static class DnsAbstraction
2525
/// <summary>
2626
/// Returns the Internet Protocol (IP) addresses for the specified host.
2727
/// </summary>
28-
/// <param name="hostNameOrAddress">The host name or IP address to resolve</param>
28+
/// <param name="hostNameOrAddress">The host name or IP address to resolve.</param>
2929
/// <returns>
3030
/// An array of type <see cref="IPAddress"/> that holds the IP addresses for the host that
3131
/// is specified by the <paramref name="hostNameOrAddress"/> parameter.
@@ -34,7 +34,7 @@ internal static class DnsAbstraction
3434
/// <exception cref="SocketException">An error is encountered when resolving <paramref name="hostNameOrAddress"/>.</exception>
3535
public static IPAddress[] GetHostAddresses(string hostNameOrAddress)
3636
{
37-
// TODO Eliminate sync variant, and implement timeout
37+
/* TODO Eliminate sync variant, and implement timeout */
3838

3939
#if FEATURE_DNS_SYNC
4040
return Dns.GetHostAddresses(hostNameOrAddress);
@@ -92,7 +92,7 @@ public static IPAddress[] GetHostAddresses(string hostNameOrAddress)
9292
/// <summary>
9393
/// Returns the Internet Protocol (IP) addresses for the specified host.
9494
/// </summary>
95-
/// <param name="hostNameOrAddress">The host name or IP address to resolve</param>
95+
/// <param name="hostNameOrAddress">The host name or IP address to resolve.</param>
9696
/// <returns>
9797
/// A task with result of an array of type <see cref="IPAddress"/> that holds the IP addresses for the host that
9898
/// is specified by the <paramref name="hostNameOrAddress"/> parameter.

src/Renci.SshNet/Abstractions/ReflectionAbstraction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Renci.SshNet.Abstractions
77
internal static class ReflectionAbstraction
88
{
99
public static IEnumerable<T> GetCustomAttributes<T>(this Type type, bool inherit)
10-
where T:Attribute
10+
where T : Attribute
1111
{
1212
var attributes = type.GetCustomAttributes(typeof(T), inherit);
1313
return attributes.Cast<T>();

src/Renci.SshNet/Abstractions/SocketAbstraction.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,6 @@ public static byte[] Read(Socket socket, int size, TimeSpan timeout)
261261
return buffer;
262262
}
263263

264-
public static Task<int> ReadAsync(Socket socket, byte[] buffer, int offset, int length, CancellationToken cancellationToken)
265-
{
266-
return socket.ReceiveAsync(buffer, offset, length, cancellationToken);
267-
}
268-
269264
/// <summary>
270265
/// Receives data from a bound <see cref="Socket"/> into a receive buffer.
271266
/// </summary>
@@ -293,7 +288,7 @@ public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeS
293288
var totalBytesRead = 0;
294289
var totalBytesToRead = size;
295290

296-
socket.ReceiveTimeout = (int)readTimeout.TotalMilliseconds;
291+
socket.ReceiveTimeout = (int) readTimeout.TotalMilliseconds;
297292

298293
do
299294
{
@@ -330,6 +325,11 @@ public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeS
330325
return totalBytesRead;
331326
}
332327

328+
public static Task<int> ReadAsync(Socket socket, byte[] buffer, int offset, int length, CancellationToken cancellationToken)
329+
{
330+
return socket.ReceiveAsync(buffer, offset, length, cancellationToken);
331+
}
332+
333333
public static void Send(Socket socket, byte[] data)
334334
{
335335
Send(socket, data, 0, data.Length);

0 commit comments

Comments
 (0)