Skip to content

Commit

Permalink
Static analysis: deconstruction (#26969)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers authored Dec 14, 2021
1 parent 70cd929 commit 4a2e4eb
Show file tree
Hide file tree
Showing 48 changed files with 414 additions and 450 deletions.
1 change: 1 addition & 0 deletions All.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EJavaScript_002ECodeStyle_002ESettingsUpgrade_002EJsWrapperSettingsUpgrader/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EXml_002ECodeStyle_002EFormatSettingsUpgrade_002EXmlMoveToCommonFormatterSettingsUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=annotatable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=annotatables/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=autoscale/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=batchable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=composability/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TEleme
return true;
}

foreach (var aPair in aDict)
foreach (var (key, element) in aDict)
{
if (!bDict.TryGetValue(aPair.Key, out var bValue))
if (!bDict.TryGetValue(key, out var bValue))
{
return false;
}

if (aPair.Value is null)
if (element is null)
{
if (bValue is null)
{
Expand All @@ -70,7 +70,7 @@ private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TEleme
return false;
}

if (bValue is null || !elementComparer.Equals(aPair.Value, bValue))
if (bValue is null || !elementComparer.Equals(element, bValue))
{
return false;
}
Expand All @@ -83,10 +83,10 @@ private static int GetHashCode(TCollection source, ValueComparer<TElement> eleme
{
var nullableEqualityComparer = new NullableEqualityComparer<TElement>(elementComparer);
var hash = new HashCode();
foreach (var el in source)
foreach (var (key, element) in source)
{
hash.Add(el.Key);
hash.Add(el.Value, nullableEqualityComparer);
hash.Add(key);
hash.Add(element, nullableEqualityComparer);
}

return hash.ToHashCode();
Expand All @@ -100,9 +100,9 @@ private static TCollection Snapshot(TCollection source, ValueComparer<TElement>
}

var snapshot = new Dictionary<string, TElement?>(((IReadOnlyDictionary<string, TElement?>)source).Count);
foreach (var e in source)
foreach (var (key, element) in source)
{
snapshot.Add(e.Key, e.Value is null ? null : elementComparer.Snapshot(e.Value.Value));
snapshot.Add(key, element is null ? null : elementComparer.Snapshot(element.Value));
}

return (TCollection)(object)snapshot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TEleme
return true;
}

foreach (var aPair in aDict)
foreach (var (key, element) in aDict)
{
if (!bDict.TryGetValue(aPair.Key, out var bValue)
|| !elementComparer.Equals(aPair.Value, bValue))
if (!bDict.TryGetValue(key, out var bValue)
|| !elementComparer.Equals(element, bValue))
{
return false;
}
Expand All @@ -67,10 +67,10 @@ private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TEleme
private static int GetHashCode(TCollection source, ValueComparer<TElement> elementComparer)
{
var hash = new HashCode();
foreach (var el in source)
foreach (var (key, element) in source)
{
hash.Add(el.Key);
hash.Add(el.Value, elementComparer);
hash.Add(key);
hash.Add(element, elementComparer);
}

return hash.ToHashCode();
Expand All @@ -84,9 +84,9 @@ private static TCollection Snapshot(TCollection source, ValueComparer<TElement>
}

var snapshot = new Dictionary<string, TElement>(((IReadOnlyDictionary<string, TElement>)source).Count);
foreach (var e in source)
foreach (var (key, element) in source)
{
snapshot.Add(e.Key, e.Value is null ? default! : elementComparer.Snapshot(e.Value));
snapshot.Add(key, element is null ? default! : elementComparer.Snapshot(element));
}

return (TCollection)(object)snapshot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ protected virtual void ValidateSharedContainerCompatibility(
mappedTypes.Add(entityType);
}

foreach (var containerMapping in containers)
foreach (var (container, mappedTypes) in containers)
{
var mappedTypes = containerMapping.Value;
var container = containerMapping.Key;
ValidateSharedContainerCompatibility(mappedTypes, container, logger);
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/EFCore.Cosmos/Query/Internal/SelectExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ public virtual void ApplyProjection()
}

var result = new Dictionary<ProjectionMember, Expression>();
foreach (var keyValuePair in _projectionMapping)
foreach (var (projectionMember, expression) in _projectionMapping)
{
result[keyValuePair.Key] = Constant(
result[projectionMember] = Constant(
AddToProjection(
keyValuePair.Value,
keyValuePair.Key.Last?.Name));
expression,
projectionMember.Last?.Name));
}

_projectionMapping = result;
Expand All @@ -221,9 +221,9 @@ public virtual void ApplyProjection()
public virtual void ReplaceProjectionMapping(IDictionary<ProjectionMember, Expression> projectionMapping)
{
_projectionMapping.Clear();
foreach (var kvp in projectionMapping)
foreach (var (projectionMember, expression) in projectionMapping)
{
_projectionMapping[kvp.Key] = kvp.Value;
_projectionMapping[projectionMember] = expression;
}
}

Expand Down Expand Up @@ -458,12 +458,12 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
else
{
projectionMapping = new Dictionary<ProjectionMember, Expression>();
foreach (var mapping in _projectionMapping)
foreach (var (projectionMember, expression) in _projectionMapping)
{
var newProjection = visitor.Visit(mapping.Value);
changed |= newProjection != mapping.Value;
var newProjection = visitor.Visit(expression);
changed |= newProjection != expression;

projectionMapping[mapping.Key] = newProjection;
projectionMapping[projectionMember] = newProjection;
}
}

Expand Down Expand Up @@ -519,9 +519,9 @@ public virtual SelectExpression Update(
SqlExpression? offset)
{
var projectionMapping = new Dictionary<ProjectionMember, Expression>();
foreach (var kvp in _projectionMapping)
foreach (var (projectionMember, expression) in _projectionMapping)
{
projectionMapping[kvp.Key] = kvp.Value;
projectionMapping[projectionMember] = expression;
}

return new SelectExpression(projections, fromExpression, orderings, Container)
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ private static async Task<bool> CreateContainerIfNotExistsOnceAsync(
(ContainerProperties Parameters, CosmosClientWrapper Wrapper) parametersTuple,
CancellationToken cancellationToken = default)
{
var parameters = parametersTuple.Parameters;
var wrapper = parametersTuple.Wrapper;
var (parameters, wrapper) = parametersTuple;
using var response = await wrapper.Client.GetDatabase(wrapper._databaseId).CreateContainerStreamAsync(
new Azure.Cosmos.ContainerProperties(parameters.Id, "/" + parameters.PartitionKey)
{
Expand Down
4 changes: 1 addition & 3 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ private static IEnumerable<ContainerProperties> GetContainersToCreate(IModel mod
mappedTypes.Add(entityType);
}

foreach (var containerMapping in containers)
foreach (var (containerName, mappedTypes) in containers)
{
var mappedTypes = containerMapping.Value;
var containerName = containerMapping.Key;
string? partitionKey = null;
int? analyticalTtl = null;
int? defaultTtl = null;
Expand Down
6 changes: 3 additions & 3 deletions src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public virtual ScaffoldedMigration ScaffoldMigration(
subNamespace = "Migrations";
}

var lastMigration = Dependencies.MigrationsAssembly.Migrations.LastOrDefault();
var (key, typeInfo) = Dependencies.MigrationsAssembly.Migrations.LastOrDefault();

var migrationNamespace =
(!string.IsNullOrEmpty(rootNamespace)
Expand All @@ -93,7 +93,7 @@ public virtual ScaffoldedMigration ScaffoldMigration(

if (subNamespaceDefaulted)
{
migrationNamespace = GetNamespace(lastMigration.Value?.AsType(), migrationNamespace!);
migrationNamespace = GetNamespace(typeInfo?.AsType(), migrationNamespace!);
}

var sanitizedContextName = _contextType.Name;
Expand Down Expand Up @@ -184,7 +184,7 @@ public virtual ScaffoldedMigration ScaffoldMigration(

return new ScaffoldedMigration(
codeGenerator.FileExtension,
lastMigration.Key,
key,
migrationCode,
migrationId,
migrationMetadataCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public virtual IReadOnlyCollection<ScaffoldedFile> GenerateModel(
var modelBuilderFileName = options.ContextType.ShortDisplayName() + ModelBuilderSuffix + FileExtension;
scaffoldedFiles.Add(new ScaffoldedFile { Path = modelBuilderFileName, Code = modelBuilderCode });

foreach (var (entityType, namePair) in entityTypeIds)
foreach (var (entityType, (_, @class)) in entityTypeIds)
{
var generatedCode = GenerateEntityType(
entityType, options.ModelNamespace, namePair.Class, options.UseNullableReferenceTypes);
entityType, options.ModelNamespace, @class, options.UseNullableReferenceTypes);

var entityTypeFileName = namePair.Class + FileExtension;
var entityTypeFileName = @class + FileExtension;
scaffoldedFiles.Add(new ScaffoldedFile { Path = entityTypeFileName, Code = generatedCode });
}

Expand Down
21 changes: 10 additions & 11 deletions src/EFCore.InMemory/Query/Internal/EntityProjectionExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ public virtual EntityProjectionExpression UpdateEntityType(IEntityType derivedTy
}

var readExpressionMap = new Dictionary<IProperty, MethodCallExpression>();
foreach (var kvp in _readExpressionMap)
foreach (var (property, methodCallExpression) in _readExpressionMap)
{
var property = kvp.Key;
if (derivedType.IsAssignableFrom(property.DeclaringEntityType)
|| property.DeclaringEntityType.IsAssignableFrom(derivedType))
{
readExpressionMap[property] = kvp.Value;
readExpressionMap[property] = methodCallExpression;
}
}

Expand Down Expand Up @@ -151,12 +150,12 @@ public virtual EntityProjectionExpression Clone()
{
var readExpressionMap = new Dictionary<IProperty, MethodCallExpression>(_readExpressionMap);
var entityProjectionExpression = new EntityProjectionExpression(EntityType, readExpressionMap);
foreach (var kvp in _navigationExpressionsCache)
foreach (var (navigation, entityShaperExpression) in _navigationExpressionsCache)
{
entityProjectionExpression._navigationExpressionsCache[kvp.Key] = new EntityShaperExpression(
kvp.Value.EntityType,
((EntityProjectionExpression)kvp.Value.ValueBufferExpression).Clone(),
kvp.Value.IsNullable);
entityProjectionExpression._navigationExpressionsCache[navigation] = new EntityShaperExpression(
entityShaperExpression.EntityType,
((EntityProjectionExpression)entityShaperExpression.ValueBufferExpression).Clone(),
entityShaperExpression.IsNullable);
}

return entityProjectionExpression;
Expand All @@ -173,10 +172,10 @@ void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)
expressionPrinter.AppendLine(nameof(EntityProjectionExpression) + ":");
using (expressionPrinter.Indent())
{
foreach (var readExpressionMapEntry in _readExpressionMap)
foreach (var (property, methodCallExpression) in _readExpressionMap)
{
expressionPrinter.Append(readExpressionMapEntry.Key + " -> ");
expressionPrinter.Visit(readExpressionMapEntry.Value);
expressionPrinter.Append(property + " -> ");
expressionPrinter.Visit(methodCallExpression);
expressionPrinter.AppendLine();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ private sealed class CloningExpressionVisitor : ExpressionVisitor
inMemoryQueryExpression._clientProjections.Select(e => Visit(e)));
clonedInMemoryQueryExpression._projectionMappingExpressions.AddRange(
inMemoryQueryExpression._projectionMappingExpressions);
foreach (var item in inMemoryQueryExpression._projectionMapping)
foreach (var (projectionMember, value) in inMemoryQueryExpression._projectionMapping)
{
clonedInMemoryQueryExpression._projectionMapping[item.Key] = Visit(item.Value);
clonedInMemoryQueryExpression._projectionMapping[projectionMember] = Visit(value);
}

return clonedInMemoryQueryExpression;
Expand Down
Loading

0 comments on commit 4a2e4eb

Please sign in to comment.