Skip to content
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

Style Checking for Core #7581

Merged
merged 1 commit into from
Sep 13, 2019
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions sdk/core/Azure.Core/src/AccessToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ public AccessToken(string accessToken, DateTimeOffset expiresOn)

public override bool Equals(object obj)
{
if (obj is AccessToken)
if (obj is AccessToken accessToken)
{
var accessToken = (AccessToken)obj;

return accessToken.ExpiresOn == ExpiresOn && accessToken.Token == Token;
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/core/Azure.Core/src/AsyncCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class AsyncCollection<T> : IAsyncEnumerable<Response<T>> where T
/// class for mocking.
/// </summary>
protected AsyncCollection() =>
this.CancellationToken = CancellationToken.None;
CancellationToken = CancellationToken.None;

/// <summary>
/// Initializes a new instance of the <see cref="AsyncCollection{T}"/>
Expand All @@ -37,7 +37,7 @@ protected AsyncCollection() =>
/// enumerating asynchronously.
/// </param>
protected AsyncCollection(CancellationToken cancellationToken) =>
this.CancellationToken = cancellationToken;
CancellationToken = cancellationToken;

/// <summary>
/// Enumerate the values a <see cref="Page{T}"/> at a time. This may
Expand Down
6 changes: 3 additions & 3 deletions sdk/core/Azure.Core/src/Buffers/StreamMemoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static async Task WriteAsync(this Stream stream, ReadOnlyMemory<byte> buf
byte[]? array = null;
try
{
if (MemoryMarshal.TryGetArray(buffer, out var arraySegment))
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> arraySegment))
{
await stream.WriteAsync(arraySegment.Array, arraySegment.Offset, arraySegment.Count, cancellation).ConfigureAwait(false);
}
Expand Down Expand Up @@ -57,9 +57,9 @@ public static async Task WriteAsync(this Stream stream, ReadOnlySequence<byte> b
byte[]? array = null;
try
{
foreach (var segment in buffer)
foreach (ReadOnlyMemory<byte> segment in buffer)
{
if (MemoryMarshal.TryGetArray(segment, out var arraySegment))
if (MemoryMarshal.TryGetArray(segment, out ArraySegment<byte> arraySegment))
{
await stream.WriteAsync(arraySegment.Array, arraySegment.Offset, arraySegment.Count, cancellation).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ internal sealed class HttpPipelineEventSource : EventSource

private HttpPipelineEventSource() : base(EventSourceName) { }

internal static readonly HttpPipelineEventSource Singleton = new HttpPipelineEventSource();
private static readonly HttpPipelineEventSource s_singleton = new HttpPipelineEventSource();

public static HttpPipelineEventSource Singleton
{
get { return s_singleton; }
}

// TODO (pri 2): this logs just the URI. We need more
[NonEvent]
Expand Down Expand Up @@ -428,7 +433,7 @@ private static byte[] FormatContent(byte[] buffer, int offset, int length)
private static string FormatHeaders(IEnumerable<HttpHeader> headers)
{
var stringBuilder = new StringBuilder();
foreach (var header in headers)
foreach (HttpHeader header in headers)
{
stringBuilder.AppendLine(header.ToString());
}
Expand Down
17 changes: 6 additions & 11 deletions sdk/core/Azure.Core/src/Http/HttpClientTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public HttpClientTransport() : this(CreateDefaultClient())

public HttpClientTransport(HttpClient client)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
_client = client;
_client = client ?? throw new ArgumentNullException(nameof(client));
}

public static readonly HttpClientTransport Shared = new HttpClientTransport();
Expand Down Expand Up @@ -66,8 +62,7 @@ private static HttpClient CreateDefaultClient()

private static HttpRequestMessage BuildRequestMessage(HttpPipelineMessage message)
{
var pipelineRequest = message.Request as PipelineRequest;
if (pipelineRequest == null)
if (!(message.Request is PipelineRequest pipelineRequest))
{
throw new InvalidOperationException("the request is not compatible with the transport");
}
Expand All @@ -93,14 +88,14 @@ internal static bool TryGetHeader(HttpHeaders headers, HttpContent? content, str

internal static IEnumerable<HttpHeader> GetHeaders(HttpHeaders headers, HttpContent? content)
{
foreach (var header in headers)
foreach (KeyValuePair<string, IEnumerable<string>> header in headers)
{
yield return new HttpHeader(header.Key, JoinHeaderValues(header.Value));
}

if (content != null)
{
foreach (var header in content.Headers)
foreach (KeyValuePair<string, IEnumerable<string>> header in content.Headers)
{
yield return new HttpHeader(header.Key, JoinHeaderValues(header.Value));
}
Expand Down Expand Up @@ -131,7 +126,7 @@ internal static bool ContainsHeader(HttpHeaders headers, HttpContent? content, s

internal static void CopyHeaders(HttpHeaders from, HttpHeaders to)
{
foreach (var header in from)
foreach (KeyValuePair<string, IEnumerable<string>> header in from)
{
if (!to.TryAddWithoutValidation(header.Key, header.Value))
{
Expand Down Expand Up @@ -175,7 +170,7 @@ protected internal override void AddHeader(string name, string value)
return;
}

var requestContent = EnsureContentInitialized();
PipelineContentAdapter requestContent = EnsureContentInitialized();
if (!requestContent.Headers.TryAddWithoutValidation(name, value))
{
throw new InvalidOperationException("Unable to add header to request or content");
Expand Down
14 changes: 7 additions & 7 deletions sdk/core/Azure.Core/src/Http/HttpRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public HttpRange(long? offset = default, long? count = default)
if (count.HasValue && count <= 0)
throw new ArgumentOutOfRangeException(nameof(count));

this.Offset = offset ?? 0;
this.Count = count;
Offset = offset ?? 0;
Count = count;
}

/// <summary>
Expand All @@ -52,12 +52,12 @@ public override string ToString()
{
// No additional validation by design. API can validate parameter by case, and use this method.
var endRange = "";
if (this.Count.HasValue && this.Count != 0)
if (Count.HasValue && Count != 0)
{
endRange = (this.Offset + this.Count.Value - 1).ToString(CultureInfo.InvariantCulture);
endRange = (Offset + Count.Value - 1).ToString(CultureInfo.InvariantCulture);
}

return FormattableString.Invariant($"{Unit}={this.Offset}-{endRange}");
return FormattableString.Invariant($"{Unit}={Offset}-{endRange}");
}

/// <summary>
Expand Down Expand Up @@ -87,15 +87,15 @@ public override string ToString()
/// </summary>
/// <param name="other">The instance to compare to.</param>
/// <returns>True if they're equal, false otherwise.</returns>
public bool Equals(HttpRange other) => this.Offset == other.Offset && this.Count == other.Count;
public bool Equals(HttpRange other) => Offset == other.Offset && Count == other.Count;

/// <summary>
/// Check if two <see cref="HttpRange"/> instances are equal.
/// </summary>
/// <param name="obj">The instance to compare to.</param>
/// <returns>True if they're equal, false otherwise.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => obj is HttpRange other && this.Equals(other);
public override bool Equals(object obj) => obj is HttpRange other && Equals(other);

/// <summary>
/// Get a hash code for the <see cref="HttpRange"/>.
Expand Down
8 changes: 4 additions & 4 deletions sdk/core/Azure.Core/src/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public readonly struct Page<T>
/// Gets the <see cref="Response"/> that provided this
/// <see cref="Page{T}"/>.
/// </summary>
public Response GetRawResponse() => this._response;
public Response GetRawResponse() => _response;

/// <summary>
/// Creates a new <see cref="Page{T}"/>.
Expand All @@ -50,9 +50,9 @@ public readonly struct Page<T>
/// </param>
public Page(IReadOnlyList<T> values, string? continuationToken, Response response)
{
this.Values = values;
this.ContinuationToken = continuationToken;
this._response = response;
Values = values;
ContinuationToken = continuationToken;
_response = response;
}

/// <summary>
Expand Down
18 changes: 9 additions & 9 deletions sdk/core/Azure.Core/src/Pipeline/ActivityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ namespace Azure.Core.Pipeline
/// </summary>
internal static class ActivityExtensions
{
private static readonly MethodInfo? SetIdFormatMethod = typeof(Activity).GetMethod("SetIdFormat");
private static readonly MethodInfo? GetIdFormatMethod = typeof(Activity).GetProperty("IdFormat")?.GetMethod;
private static readonly MethodInfo? GetTraceStateStringMethod = typeof(Activity).GetProperty("TraceStateString")?.GetMethod;
private static readonly MethodInfo? s_setIdFormatMethod = typeof(Activity).GetMethod("SetIdFormat");
private static readonly MethodInfo? s_getIdFormatMethod = typeof(Activity).GetProperty("IdFormat")?.GetMethod;
private static readonly MethodInfo? s_getTraceStateStringMethod = typeof(Activity).GetProperty("TraceStateString")?.GetMethod;

public static bool SetW3CFormat(this Activity activity)
{
if (SetIdFormatMethod == null) return false;
if (s_setIdFormatMethod == null) return false;

SetIdFormatMethod.Invoke(activity, new object[]{ 2 /* ActivityIdFormat.W3C */});
s_setIdFormatMethod.Invoke(activity, new object[]{ 2 /* ActivityIdFormat.W3C */});

return true;
}

public static bool IsW3CFormat(this Activity activity)
{
if (GetIdFormatMethod == null) return false;
if (s_getIdFormatMethod == null) return false;

object result = GetIdFormatMethod.Invoke(activity, Array.Empty<object>());
object result = s_getIdFormatMethod.Invoke(activity, Array.Empty<object>());

return (int)result == 2 /* ActivityIdFormat.W3C */;
}
Expand All @@ -39,9 +39,9 @@ public static bool TryGetTraceState(this Activity activity, out string? traceSta
{
traceState = null;

if (GetTraceStateStringMethod == null) return false;
if (s_getTraceStateStringMethod == null) return false;

traceState = GetTraceStateStringMethod.Invoke(activity, Array.Empty<object>()) as string;
traceState = s_getTraceStateStringMethod.Invoke(activity, Array.Empty<object>()) as string;

return true;
}
Expand Down
10 changes: 5 additions & 5 deletions sdk/core/Azure.Core/src/Pipeline/HttpEnvironmentProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ internal sealed partial class HttpEnvironmentProxy : IWebProxy
private const string EnvNoProxyUC = "NO_PROXY";
private const string EnvCGI = "GATEWAY_INTERFACE"; // Running in a CGI environment.

private Uri _httpProxyUri; // String URI for HTTP requests
private Uri _httpsProxyUri; // String URI for HTTPS requests
private string[] _bypass;// list of domains not to proxy
private ICredentials _credentials;
private readonly Uri _httpProxyUri; // String URI for HTTP requests
private readonly Uri _httpsProxyUri; // String URI for HTTPS requests
private readonly string[] _bypass;// list of domains not to proxy
private readonly ICredentials _credentials;

public static bool TryCreate(out IWebProxy proxy)
{
Expand Down Expand Up @@ -200,7 +200,6 @@ private static Uri GetUriFromString(string value)
string user = null;
string password = null;
ushort port = 80;
string host = null;

// Check if there is authentication part with user and possibly password.
// Curl accepts raw text and that may break URI parser.
Expand Down Expand Up @@ -233,6 +232,7 @@ private static Uri GetUriFromString(string value)

int ipV6AddressEnd = value.IndexOf(']');
separatorIndex = value.LastIndexOf(':');
string host;
// No ':' or it is part of IPv6 address.
if (separatorIndex == -1 || (ipV6AddressEnd != -1 && separatorIndex < ipV6AddressEnd))
{
Expand Down
12 changes: 7 additions & 5 deletions sdk/core/Azure.Core/src/Pipeline/HttpPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ public Response SendRequest(Request request, bool bufferResponse, CancellationTo

private HttpPipelineMessage BuildMessage(Request request, bool bufferResponse, CancellationToken cancellationToken)
{
var message = new HttpPipelineMessage(request, _responseClassifier, cancellationToken);
message.Request = request;
message.BufferResponse = bufferResponse;
message.ResponseClassifier = _responseClassifier;
var message = new HttpPipelineMessage(request, _responseClassifier, cancellationToken)
{
Request = request,
BufferResponse = bufferResponse,
ResponseClassifier = _responseClassifier
};
return message;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private sealed class StreamContent : HttpPipelineRequestContent
{
private const int CopyToBufferSize = 81920;

private Stream _stream;
private readonly Stream _stream;

private readonly long _origin;

Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/src/Pipeline/RequestActivityPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static async Task ProcessAsync(HttpPipelineMessage message, ReadOnlyMemo

private static async Task ProcessNextAsync(HttpPipelineMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool isAsync)
{
var currentActivity = Activity.Current;
Activity currentActivity = Activity.Current;

if (currentActivity != null)
{
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/src/Pipeline/RetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected virtual TimeSpan GetServerDelay(HttpPipelineMessage message)
{
return TimeSpan.FromSeconds(delaySeconds);
}
if (DateTimeOffset.TryParse(retryAfterValue, out var delayTime))
if (DateTimeOffset.TryParse(retryAfterValue, out DateTimeOffset delayTime))
{
return delayTime - DateTimeOffset.Now;
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/src/ResponseExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static async Task<string> CreateRequestFailedMessageAsync(string message,
messageBuilder
.AppendLine()
.AppendLine("Headers:");
foreach (var responseHeader in response.Headers)
foreach (Http.HttpHeader responseHeader in response.Headers)
{
messageBuilder.AppendLine($"{responseHeader.Name}: {responseHeader.Value}");
}
Expand Down
Loading