Skip to content

Fix analyzer errors #1221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
32 changes: 32 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ dotnet_diagnostic.S1172.severity = none
# This is a duplicate of IDE0059.
dotnet_diagnostic.S1481.severity = none

# S1854: Unused assignments should be removed
# https://rules.sonarsource.com/csharp/RSPEC-1854
#
# This is a duplicate of IDE0059.
dotnet_diagnostic.S1854.severity = none

# S2259: Null pointers should not be dereferenced
# https://rules.sonarsource.com/csharp/RSPEC-2259
#
Expand Down Expand Up @@ -168,6 +174,12 @@ dotnet_diagnostic.S3928.severity = none
# This is a duplicate of CA2002, and partial duplicate of MA0064.
dotnet_diagnostic.S3998.severity = none

# S4070: Non-flags enums should not be marked with "FlagsAttribute"
# https://rules.sonarsource.com/csharp/RSPEC-4070
#
# This is a duplicate of MA0062.
dotnet_diagnostic.S4070.severity = none

# S4456: Parameter validation in yielding methods should be wrapped
# https://rules.sonarsource.com/csharp/RSPEC-4456
#
Expand Down Expand Up @@ -517,6 +529,10 @@ dotnet_diagnostic.CA2208.severity = none

#### Roslyn IDE analyser rules ####

# IDE0028: Use collection initializers
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0028
dotnet_diagnostic.IDE0028.severity = none

# IDE0032: Use auto-implemented property
#
# For performance reasons, we do not always want to enforce the use of
Expand Down Expand Up @@ -571,6 +587,22 @@ dotnet_diagnostic.IDE0130.severity = none
# 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);
dotnet_diagnostic.IDE0270.severity = none

# IDE0290: Use primary constructor
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0290
dotnet_diagnostic.IDE0290.severity = none

# IDE0300: Collection initialization can be simplified
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0300
#
# TODO: Discuss whether we want to start using this
dotnet_diagnostic.IDE0300.severity = none

# IDE0301: Simplify collection initialization
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0301
#
# TODO: Discuss whether we want to start using this
dotnet_diagnostic.IDE0301.severity = none

#### .NET Compiler Platform code style rules ####

### Language rules ###
Expand Down
4 changes: 1 addition & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Code analysis properties.
-->
<PropertyGroup>
<EnableNETAnalyzers>false</EnableNETAnalyzers>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>preview-All</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Expand All @@ -34,11 +34,9 @@
Use fixed version of analyzers.
-->
<ItemGroup>
<!--
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0-preview1.23165.1" PrivateAssets="all" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="all" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.54" PrivateAssets="all" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.55.0.65544" PrivateAssets="all" />
-->
</ItemGroup>
</Project>
64 changes: 64 additions & 0 deletions src/Renci.SshNet/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[*.cs]

# S1264: A "while" loop should be used instead of a "for" loop
# https://rules.sonarsource.com/csharp/RSPEC-1264
dotnet_diagnostic.S1264.severity = none

#### SYSLIB diagnostics ####

# SYSLIB1045: Use 'GeneratedRegexAttribute' to generate the regular expression implementation at compile-time
Expand All @@ -9,30 +13,90 @@ dotnet_diagnostic.SYSLIB1045.severity = none

### StyleCop Analyzers rules ###

# SA1123: Do not place regions within elements
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1123.md
dotnet_diagnostic.SA1123.severity = none

# SA1124: Do not use regions
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1124.md
dotnet_diagnostic.SA1124.severity = none

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

# SA1204: Static elements must appear before instance elements
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1204.md
dotnet_diagnostic.SA1204.severity = none

# SA1310: Field names must not contain underscore
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1310.md
#dotnet_diagnostic.SA1310.severity = none

# SA1636: File header copyright text should match
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1636.md
dotnet_diagnostic.SA1636.severity = none

#### Meziantou.Analyzer rules ####

# MA0011: IFormatProvider is missing
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0011.md
#
# TODO: Remove exclusion when issues are fixed
dotnet_diagnostic.MA0011.severity = none

# MA0015: Specify the parameter name in ArgumentException
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0015.md
#
# TODO: Remove exclusion when issues are fixed
dotnet_diagnostic.MA0015.severity = none

# MA0026: Fix TODO comment
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0026.md
dotnet_diagnostic.MA0026.severity = none

# MA0053: Make class sealed
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0053.md
MA0053.public_class_should_be_sealed = false

# MA0055: Do not use finalizer
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0055.md
#
# TODO: Remove exclusion when issues are fixed
dotnet_diagnostic.MA0055.severity = none

# MA0110: Use the Regex source generator
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0110.md
dotnet_diagnostic.MA0110.severity = none

#### .NET Compiler Platform analysers rules ####

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

# CA1062: Validate arguments of public methods
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1062
#
# TODO: Remove exclusion when issues are fixed
dotnet_diagnostic.CA1062.severity = none

# CA2213: Disposable fields should be disposed
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2213
dotnet_diagnostic.CA2213.severity = none

# CA3075: Insecure DTD Processing
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca3075
dotnet_diagnostic.CA3075.severity = none

# IDE0004: Types that own disposable fields should be disposable
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0004
dotnet_diagnostic.IDE0004.severity = none

# IDE0048: Add parentheses for clarity
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0047
dotnet_diagnostic.IDE0048.severity = none

# IDE0305: Collection initialization can be simplified
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0305
dotnet_diagnostic.IDE0305.severity = none
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Abstractions/CryptoAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static byte[] GenerateRandom(int length)
/// Fills an array of bytes with a cryptographically strong random sequence of values.
/// </summary>
/// <param name="data">The array to fill with cryptographically strong random bytes.</param>
/// <exception cref="ArgumentNullException"><paramref name="data"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
/// <remarks>
/// The length of the byte array determines how many random bytes are produced.
/// </remarks>
Expand Down
4 changes: 0 additions & 4 deletions src/Renci.SshNet/Abstractions/DiagnosticAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ public static bool IsEnabled(TraceEventType traceEventType)
public static void Log(string text)
{
Loggging.TraceEvent(TraceEventType.Verbose,
#if NET6_0_OR_GREATER
System.Environment.CurrentManagedThreadId,
#else
System.Threading.Thread.CurrentThread.ManagedThreadId,
#endif // NET6_0_OR_GREATER
text);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Renci.SshNet/Abstractions/DnsAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ internal static class DnsAbstraction
/// <summary>
/// Returns the Internet Protocol (IP) addresses for the specified host.
/// </summary>
/// <param name="hostNameOrAddress">The host name or IP address to resolve</param>
/// <param name="hostNameOrAddress">The host name or IP address to resolve.</param>
/// <returns>
/// An array of type <see cref="IPAddress"/> that holds the IP addresses for the host that
/// is specified by the <paramref name="hostNameOrAddress"/> parameter.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="hostNameOrAddress"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="hostNameOrAddress"/> is <see langword="null"/>.</exception>
/// <exception cref="SocketException">An error is encountered when resolving <paramref name="hostNameOrAddress"/>.</exception>
public static IPAddress[] GetHostAddresses(string hostNameOrAddress)
{
// TODO Eliminate sync variant, and implement timeout
/* TODO Eliminate sync variant, and implement timeout */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't understand this change (would prefer // as is used everywhere else)


#if FEATURE_DNS_SYNC
return Dns.GetHostAddresses(hostNameOrAddress);
Expand Down Expand Up @@ -92,12 +92,12 @@ public static IPAddress[] GetHostAddresses(string hostNameOrAddress)
/// <summary>
/// Returns the Internet Protocol (IP) addresses for the specified host.
/// </summary>
/// <param name="hostNameOrAddress">The host name or IP address to resolve</param>
/// <param name="hostNameOrAddress">The host name or IP address to resolve.</param>
/// <returns>
/// A task with result of an array of type <see cref="IPAddress"/> that holds the IP addresses for the host that
/// is specified by the <paramref name="hostNameOrAddress"/> parameter.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="hostNameOrAddress"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="hostNameOrAddress"/> is <see langword="null"/>.</exception>
/// <exception cref="SocketException">An error is encountered when resolving <paramref name="hostNameOrAddress"/>.</exception>
public static Task<IPAddress[]> GetHostAddressesAsync(string hostNameOrAddress)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Abstractions/ReflectionAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Renci.SshNet.Abstractions
internal static class ReflectionAbstraction
{
public static IEnumerable<T> GetCustomAttributes<T>(this Type type, bool inherit)
where T:Attribute
where T : Attribute
{
var attributes = type.GetCustomAttributes(typeof(T), inherit);
return attributes.Cast<T>();
Expand Down
14 changes: 7 additions & 7 deletions src/Renci.SshNet/Abstractions/SocketAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static bool CanRead(Socket socket)
/// </summary>
/// <param name="socket">The <see cref="Socket"/> to check.</param>
/// <returns>
/// <c>true</c> if <paramref name="socket"/> can be written to; otherwise, <c>false</c>.
/// <see langword="true"/> if <paramref name="socket"/> can be written to; otherwise, <see langword="false"/>.
/// </returns>
public static bool CanWrite(Socket socket)
{
Expand Down Expand Up @@ -261,11 +261,6 @@ public static byte[] Read(Socket socket, int size, TimeSpan timeout)
return buffer;
}

public static Task<int> ReadAsync(Socket socket, byte[] buffer, int offset, int length, CancellationToken cancellationToken)
{
return socket.ReceiveAsync(buffer, offset, length, cancellationToken);
}

/// <summary>
/// Receives data from a bound <see cref="Socket"/> into a receive buffer.
/// </summary>
Expand Down Expand Up @@ -293,7 +288,7 @@ public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeS
var totalBytesRead = 0;
var totalBytesToRead = size;

socket.ReceiveTimeout = (int)readTimeout.TotalMilliseconds;
socket.ReceiveTimeout = (int) readTimeout.TotalMilliseconds;

do
{
Expand Down Expand Up @@ -330,6 +325,11 @@ public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeS
return totalBytesRead;
}

public static Task<int> ReadAsync(Socket socket, byte[] buffer, int offset, int length, CancellationToken cancellationToken)
{
return socket.ReceiveAsync(buffer, offset, length, cancellationToken);
}

public static void Send(Socket socket, byte[] data)
{
Send(socket, data, 0, data.Length);
Expand Down
31 changes: 22 additions & 9 deletions src/Renci.SshNet/Abstractions/SocketExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@ namespace Renci.SshNet.Abstractions
// Async helpers based on https://devblogs.microsoft.com/pfxteam/awaiting-socket-operations/
internal static class SocketExtensions
{
private sealed class SocketAsyncEventArgsAwaitable : SocketAsyncEventArgs, INotifyCompletion
private sealed class AwaitableSocketAsyncEventArgs : SocketAsyncEventArgs, INotifyCompletion
{
private static readonly Action SENTINEL = () => { };

private bool _isCancelled;
private Action _continuationAction;

public SocketAsyncEventArgsAwaitable()
public AwaitableSocketAsyncEventArgs()
{
Completed += delegate { SetCompleted(); };
Completed += (sender, e) => SetCompleted();
}

public SocketAsyncEventArgsAwaitable ExecuteAsync(Func<SocketAsyncEventArgs, bool> func)
public AwaitableSocketAsyncEventArgs ExecuteAsync(Func<SocketAsyncEventArgs, bool> func)
{
if (!func(this))
{
SetCompleted();
}

return this;
}

Expand All @@ -48,7 +49,9 @@ public void SetCancelled()
SetCompleted();
}

public SocketAsyncEventArgsAwaitable GetAwaiter()
#pragma warning disable S1144 // Unused private types or members should be removed
public AwaitableSocketAsyncEventArgs GetAwaiter()
#pragma warning restore S1144 // Unused private types or members should be removed
{
return this;
}
Expand All @@ -64,7 +67,9 @@ void INotifyCompletion.OnCompleted(Action continuation)
}
}

#pragma warning disable S1144 // Unused private types or members should be removed
public void GetResult()
#pragma warning restore S1144 // Unused private types or members should be removed
{
if (_isCancelled)
{
Expand All @@ -88,11 +93,15 @@ public static async Task ConnectAsync(this Socket socket, IPEndPoint remoteEndpo
{
cancellationToken.ThrowIfCancellationRequested();

using (var args = new SocketAsyncEventArgsAwaitable())
using (var args = new AwaitableSocketAsyncEventArgs())
{
args.RemoteEndPoint = remoteEndpoint;

using (cancellationToken.Register(o => ((SocketAsyncEventArgsAwaitable)o).SetCancelled(), args, useSynchronizationContext: false))
#if NET || NETSTANDARD2_1_OR_GREATER
await using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs)o).SetCancelled(), args, useSynchronizationContext: false).ConfigureAwait(continueOnCapturedContext: false))
#else
using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs) o).SetCancelled(), args, useSynchronizationContext: false))
#endif // NET || NETSTANDARD2_1_OR_GREATER
{
await args.ExecuteAsync(socket.ConnectAsync);
}
Expand All @@ -103,11 +112,15 @@ public static async Task<int> ReceiveAsync(this Socket socket, byte[] buffer, in
{
cancellationToken.ThrowIfCancellationRequested();

using (var args = new SocketAsyncEventArgsAwaitable())
using (var args = new AwaitableSocketAsyncEventArgs())
{
args.SetBuffer(buffer, offset, length);

using (cancellationToken.Register(o => ((SocketAsyncEventArgsAwaitable)o).SetCancelled(), args, useSynchronizationContext: false))
#if NET || NETSTANDARD2_1_OR_GREATER
await using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs) o).SetCancelled(), args, useSynchronizationContext: false).ConfigureAwait(continueOnCapturedContext: false))
#else
using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs) o).SetCancelled(), args, useSynchronizationContext: false))
#endif // NET || NETSTANDARD2_1_OR_GREATER
{
await args.ExecuteAsync(socket.ReceiveAsync);
}
Expand Down
Loading