Skip to content

Commit

Permalink
Housekeeping fix some of the code analyser warnings (#1866)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman authored Oct 6, 2024
1 parent 418092e commit f7f9c00
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
19 changes: 16 additions & 3 deletions InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ public void GenerateInterfaceStubs<TContext>(
ImmutableArray<InterfaceDeclarationSyntax> candidateInterfaces
)
{
if (compilation == null)
throw new ArgumentNullException(nameof(compilation));

if (reportDiagnostic == null)
throw new ArgumentNullException(nameof(reportDiagnostic));

if (addSource == null)
throw new ArgumentNullException(nameof(addSource));

refitInternalNamespace =
$"{refitInternalNamespace ?? string.Empty}RefitInternalGenerated";

Expand Down Expand Up @@ -131,7 +140,11 @@ ImmutableArray<InterfaceDeclarationSyntax> candidateInterfaces
m => m.ContainingType,
SymbolEqualityComparer.Default
)
.ToDictionary(g => g.Key, v => v.ToList());
.ToDictionary<IGrouping<INamedTypeSymbol, IMethodSymbol>, INamedTypeSymbol, List<IMethodSymbol>>(
g => g.Key,
v => [.. v],
SymbolEqualityComparer.Default
);

// Look through the candidate interfaces
var interfaceSymbols = new List<INamedTypeSymbol>();
Expand Down Expand Up @@ -850,9 +863,9 @@ public void Initialize(GeneratorInitializationContext context)

class SyntaxReceiver : ISyntaxReceiver

Check warning on line 864 in InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs

View workflow job for this annotation

GitHub Actions / build / build

Type 'SyntaxReceiver' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
{
public List<MethodDeclarationSyntax> CandidateMethods { get; } = new();
public List<MethodDeclarationSyntax> CandidateMethods { get; } = [];

public List<InterfaceDeclarationSyntax> CandidateInterfaces { get; } = new();
public List<InterfaceDeclarationSyntax> CandidateInterfaces { get; } = [];

public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
Expand Down
11 changes: 10 additions & 1 deletion Refit/ApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace Refit
/// Represents an error that occured while sending an API request.
/// </summary>
[Serializable]
#pragma warning disable CA1032 // Implement standard exception constructors
public class ApiException : Exception
#pragma warning restore CA1032 // Implement standard exception constructors
{
/// <summary>
/// HTTP response status code.
Expand Down Expand Up @@ -162,7 +164,12 @@ public static Task<ApiException> Create(
)
#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods
{
var exceptionMessage = CreateMessage(response.StatusCode, response.ReasonPhrase);
if (response?.IsSuccessStatusCode == true)
{
throw new ArgumentException("Response is successful, cannot create an ApiException.", nameof(response));
}

var exceptionMessage = CreateMessage(response!.StatusCode, response.ReasonPhrase);
return Create(
exceptionMessage,
message,
Expand Down Expand Up @@ -211,6 +218,7 @@ public static async Task<ApiException> Create(
return exception;
}

#pragma warning disable CA1031 // Do not catch general exception types
try
{
exception.ContentHeaders = response.Content.Headers;
Expand All @@ -235,6 +243,7 @@ public static async Task<ApiException> Create(
// so we want to make sure we don't throw another one
// that hides the real error.
}
#pragma warning restore CA1031 // Do not catch general exception types

return exception;
}
Expand Down
5 changes: 5 additions & 0 deletions Refit/CamelCaseUrlParameterKeyFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
/// </summary>
public class CamelCaseUrlParameterKeyFormatter : IUrlParameterKeyFormatter
{
/// <summary>
/// Formats the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public string Format(string key)
{
if (string.IsNullOrEmpty(key) || !char.IsUpper(key[0]))
Expand Down
7 changes: 4 additions & 3 deletions Refit/PushStreamContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ protected override async Task SerializeToStreamAsync(
{
var serializeToStreamTask = new TaskCompletionSource<bool>();

Stream wrappedStream = new CompleteTaskOnCloseStream(stream, serializeToStreamTask);
await onStreamAvailable(wrappedStream, this, context);
using Stream wrappedStream = new CompleteTaskOnCloseStream(stream, serializeToStreamTask);
await onStreamAvailable(wrappedStream, this, context).ConfigureAwait(false);

// wait for wrappedStream.Close/Dispose to get called.
await serializeToStreamTask.Task;
await serializeToStreamTask.Task.ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -170,6 +170,7 @@ TaskCompletionSource<bool> serializeToStreamTask
?? throw new ArgumentNullException(nameof(serializeToStreamTask));
}

[SuppressMessage("Usage", "CA2215:Dispose methods should call base class dispose", Justification = "We don't dispose the underlying stream because we don't own it. Dispose in this case just signifies that the user's action is finished.")]
protected override void Dispose(bool disposing)
{
// We don't dispose the underlying stream because we don't own it. Dispose in this case just signifies
Expand Down
12 changes: 6 additions & 6 deletions Refit/RestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public RestMethodInfoInternal(
if (attachmentName == null)
continue;

attachmentDict ??= new Dictionary<int, Tuple<string, string>>();
attachmentDict ??= [];
attachmentDict[i] = Tuple.Create(
attachmentName,
GetUrlNameForParameter(ParameterInfoArray[i])
Expand All @@ -146,7 +146,7 @@ public RestMethodInfoInternal(
continue;
}

queryDict ??= new Dictionary<int, string>();
queryDict ??= [];
queryDict.Add(i, GetUrlNameForParameter(ParameterInfoArray[i]));
}

Expand Down Expand Up @@ -558,12 +558,12 @@ HttpMethod method
.DeclaringType.GetInterfaces()
.SelectMany(i => i.GetTypeInfo().GetCustomAttributes(true))
.Reverse()
: Array.Empty<Attribute>();
: [];

var declaringTypeAttributes =
methodInfo.DeclaringType != null
? methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true)
: Array.Empty<Attribute>();
: [];

// Headers set on the declaring type have to come first,
// so headers set on the method can replace them. Switching
Expand All @@ -581,7 +581,7 @@ HttpMethod method
if (string.IsNullOrWhiteSpace(header))
continue;

ret ??= new Dictionary<string, string?>();
ret ??= [];

// NB: Silverlight doesn't have an overload for String.Split()
// with a count parameter, but header values can contain
Expand Down Expand Up @@ -609,7 +609,7 @@ static Dictionary<int, string> BuildHeaderParameterMap(ParameterInfo[] parameter

if (!string.IsNullOrWhiteSpace(header))
{
ret ??= new Dictionary<int, string>();
ret ??= [];
ret[i] = header.Trim();
}
}
Expand Down

0 comments on commit f7f9c00

Please sign in to comment.