Skip to content

Commit

Permalink
[Internal] Engineering: Refactors code to reduce technical debt (#2470)
Browse files Browse the repository at this point in the history
This pull request is very simple and aims to reduce the technical debt. It specifically targets the "Duplicate casts should not be made" rule in SonarQube.

Update:

As pointed out by @ealsur, the SonarQube fixes touched OSS files that probably should't be modified. To make this contribution more valuable, I fixed the following ReSharper issues:

"Replace if statement with null-propagating code" rule
"Merge sequential checks in && or || expressions" rule
  • Loading branch information
marodev authored May 17, 2021
1 parent 8f50192 commit a24d487
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ protected byte[] EncryptData(byte[] plainText, bool hasAuthenticationTag)
}
catch (Exception)
{
if (aesAlg != null)
{
aesAlg.Dispose();
}
aesAlg?.Dispose();

throw;
}
Expand Down Expand Up @@ -352,10 +349,7 @@ private byte[] DecryptData(byte[] iv, byte[] cipherText, int offset, int count)
}
catch (Exception)
{
if (aesAlg != null)
{
aesAlg.Dispose();
}
aesAlg?.Dispose();

throw;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ private static JObject RetrieveEncryptionProperties(
{
JProperty encryptionPropertiesJProp = item.Property(Constants.EncryptedInfo);
JObject encryptionPropertiesJObj = null;
if (encryptionPropertiesJProp != null && encryptionPropertiesJProp.Value != null && encryptionPropertiesJProp.Value.Type == JTokenType.Object)
if (encryptionPropertiesJProp?.Value != null && encryptionPropertiesJProp.Value.Type == JTokenType.Object)
{
encryptionPropertiesJObj = (JObject)encryptionPropertiesJProp.Value;
}
Expand Down
22 changes: 8 additions & 14 deletions Microsoft.Azure.Cosmos/src/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,10 +1353,7 @@ internal async Task<DocumentServiceResponse> ProcessRequestAsync(
{
await this.EnsureValidClientAsync(NoOpTrace.Singleton);

if (retryPolicyInstance != null)
{
retryPolicyInstance.OnBeforeSendRequest(request);
}
retryPolicyInstance?.OnBeforeSendRequest(request);

using (new ActivityScope(Guid.NewGuid()))
{
Expand Down Expand Up @@ -1728,7 +1725,7 @@ public Task<ResourceResponse<Document>> CreateDocumentAsync(string documentsFeed
private async Task<ResourceResponse<Document>> CreateDocumentInlineAsync(string documentsFeedOrDatabaseLink, object document, Documents.Client.RequestOptions options, bool disableAutomaticIdGeneration, CancellationToken cancellationToken)
{
IDocumentClientRetryPolicy requestRetryPolicy = this.ResetSessionTokenRetryPolicy.GetRequestPolicy();
if (options == null || options.PartitionKey == null)
if (options?.PartitionKey == null)
{
requestRetryPolicy = new PartitionKeyMismatchRetryPolicy(
await this.GetCollectionCacheAsync(NoOpTrace.Singleton),
Expand Down Expand Up @@ -5480,17 +5477,14 @@ private async Task<StoredProcedureResponse<TValue>> ExecuteStoredProcedurePrivat
headers))
{
request.Headers[HttpConstants.HttpHeaders.XDate] = DateTime.UtcNow.ToString("r");
if (options == null || options.PartitionKeyRangeId == null)
if (options?.PartitionKeyRangeId == null)
{
await this.AddPartitionKeyInformationAsync(
request,
options);
}

if (retryPolicyInstance != null)
{
retryPolicyInstance.OnBeforeSendRequest(request);
}
retryPolicyInstance?.OnBeforeSendRequest(request);

request.SerializerSettings = this.GetSerializerSettingsForRequest(options);
return new StoredProcedureResponse<TValue>(await this.ExecuteProcedureAsync(
Expand Down Expand Up @@ -5683,7 +5677,7 @@ public Task<ResourceResponse<Document>> UpsertDocumentAsync(string documentsFeed
private async Task<ResourceResponse<Document>> UpsertDocumentInlineAsync(string documentsFeedOrDatabaseLink, object document, Documents.Client.RequestOptions options, bool disableAutomaticIdGeneration, CancellationToken cancellationToken)
{
IDocumentClientRetryPolicy requestRetryPolicy = this.ResetSessionTokenRetryPolicy.GetRequestPolicy();
if (options == null || options.PartitionKey == null)
if (options?.PartitionKey == null)
{
requestRetryPolicy = new PartitionKeyMismatchRetryPolicy(
await this.GetCollectionCacheAsync(NoOpTrace.Singleton),
Expand Down Expand Up @@ -6646,11 +6640,11 @@ private async Task AddPartitionKeyInformationAsync(DocumentServiceRequest reques
PartitionKeyDefinition partitionKeyDefinition = collection.PartitionKey;

PartitionKeyInternal partitionKey;
if (options != null && options.PartitionKey != null && options.PartitionKey.Equals(Documents.PartitionKey.None))
if (options?.PartitionKey != null && options.PartitionKey.Equals(Documents.PartitionKey.None))
{
partitionKey = collection.GetNoneValue();
}
else if (options != null && options.PartitionKey != null)
else if (options?.PartitionKey != null)
{
partitionKey = options.PartitionKey.InternalKey;
}
Expand All @@ -6671,7 +6665,7 @@ internal async Task AddPartitionKeyInformationAsync(DocumentServiceRequest reque
// For backward compatibility, if collection doesn't have partition key defined, we assume all documents
// have empty value for it and user doesn't need to specify it explicitly.
PartitionKeyInternal partitionKey;
if (options == null || options.PartitionKey == null)
if (options?.PartitionKey == null)
{
if (partitionKeyDefinition == null || partitionKeyDefinition.Paths.Count == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static async Task<IDocumentQueryExecutionContext> CreateDocumentQueryExec
collection = await collectionCache.ResolveCollectionAsync(request, token, NoOpTrace.Singleton);
}

if (feedOptions != null && feedOptions.PartitionKey != null && feedOptions.PartitionKey.Equals(Documents.PartitionKey.None))
if (feedOptions?.PartitionKey != null && feedOptions.PartitionKey.Equals(Documents.PartitionKey.None))
{
feedOptions.PartitionKey = Documents.PartitionKey.FromInternalKey(collection.GetNoneValue());
}
Expand Down
3 changes: 1 addition & 2 deletions Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ public Task<ShouldRetryResult> ShouldRetryAsync(
Exception exception,
CancellationToken cancellationToken)
{
if (exception is DocumentClientException)
if (exception is DocumentClientException dce)
{
DocumentClientException dce = (DocumentClientException)exception;
if (!this.IsValidThrottleStatusCode(dce.StatusCode))
{
DefaultTrace.TraceError(
Expand Down
5 changes: 1 addition & 4 deletions Microsoft.Azure.Cosmos/src/Routing/ClientCollectionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ private async Task<ContainerProperties> ReadCollectionAsync(string collectionLin

using (new ActivityScope(Guid.NewGuid()))
{
if (retryPolicyInstance != null)
{
retryPolicyInstance.OnBeforeSendRequest(request);
}
retryPolicyInstance?.OnBeforeSendRequest(request);

try
{
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Routing/CollectionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private async Task<ContainerProperties> ResolveByPartitionKeyRangeIdentityAsync(
{
// if request is targeted at specific partition using x-ms-documentd-partitionkeyrangeid header,
// which contains value "<collectionrid>,<partitionkeyrangeid>", then resolve to collection rid in this header.
if (partitionKeyRangeIdentity != null && partitionKeyRangeIdentity.CollectionRid != null)
if (partitionKeyRangeIdentity?.CollectionRid != null)
{
try
{
Expand Down
5 changes: 2 additions & 3 deletions Microsoft.Azure.Cosmos/src/Routing/PartitionRoutingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public static IReadOnlyList<Range<string>> GetProvidedPartitionKeyRanges(
}

PartitionedQueryExecutionInfo queryExecutionInfo = tryGetPartitionQueryExecutionInfo.Result;
if (queryExecutionInfo == null ||
queryExecutionInfo.QueryRanges == null ||
if (queryExecutionInfo?.QueryRanges == null ||
queryExecutionInfo.QueryInfo == null ||
queryExecutionInfo.QueryRanges.Any(range => range.Min == null || range.Max == null))
{
Expand Down Expand Up @@ -435,7 +434,7 @@ public virtual Range<string> ExtractPartitionKeyRangeFromContinuationToken(IName
}
}

if (initialContinuationToken != null && initialContinuationToken.Range != null)
if (initialContinuationToken?.Range != null)
{
range = initialContinuationToken.Range;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3413,7 +3413,7 @@ public static string DumpFullExceptionMessage(Exception e)
while (e != null)
{
DocumentClientException docException = e as DocumentClientException;
if (docException != null && docException.Error != null)
if (docException?.Error != null)
{
exceptionMessage.Append("Code : " + docException.Error.Code);
if (docException.Error.ErrorDetails != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public Query(string strFilter, string rootName = "r", FieldComparer comparer = n
if (filterBuffer.Length > 0)
this.query += " WHERE " + filterBuffer.ToString();

if (comparer != null && comparer.field != null && comparer.order != 0)
if (comparer?.field != null && comparer.order != 0)
{
this.query += " ORDER BY r." + comparer.field.Name + (comparer.order > 0 ? " ASC" : " DESC");
this.Comparer = comparer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ internal static void TestForEachClient(
}
}

if (requestChargeHelper != null)
{
requestChargeHelper.CompareRequestCharge(testName);
}
requestChargeHelper?.CompareRequestCharge(testName);
}

/// <summary>
Expand Down Expand Up @@ -162,10 +159,7 @@ internal static void TestForAnyClient(
client.Dispose();
}

if (requestChargeHelper != null)
{
requestChargeHelper.CompareRequestCharge(testName);
}
requestChargeHelper?.CompareRequestCharge(testName);
}

private static void RunTestForClient(
Expand Down

0 comments on commit a24d487

Please sign in to comment.