Skip to content

Commit

Permalink
Automatic refactorings based on static analysis of EFCore project
Browse files Browse the repository at this point in the history
Mostly completely automatic; a few manual tweaks.

Part of #26805
  • Loading branch information
ajcvickers committed Nov 23, 2021
1 parent b44b8b6 commit 0b166bc
Show file tree
Hide file tree
Showing 121 changed files with 1,097 additions and 1,412 deletions.
13 changes: 13 additions & 0 deletions All.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -280,25 +280,38 @@ The .NET Foundation licenses this file to you under the MIT license.
<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/=composable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Constructible/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=doesnt/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=evaluatable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=fallbacks/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=funcletization/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Includable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=initializers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=keyless/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=materializer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=materializers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=materilization/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mins/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Multigraph/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=navigations/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Poolable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=pushdown/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=remapper/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=requiredness/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=retriable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Reuniquify/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=savepoints/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=shaper/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=spatialite/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=sproc/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=sqlite/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=subquery/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unconfigured/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unignore/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=fixup/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=attacher/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Uniquification/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Uniquified/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Uniquifier/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Uniquify/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Xunit/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public enum ChangeTrackerDebugStringOptions
/// <summary>
/// Default settings for long debug string.
/// </summary>
LongDefault = IncludeProperties | IncludeNavigations,
LongDefault = IncludeProperties | IncludeNavigations
}
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/Internal/ArrayPropertyValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void SetValue(int index, object? value)

if (value != null)
{
if (!property.ClrType.IsAssignableFrom(value.GetType()))
if (!property.ClrType.IsInstanceOfType(value))
{
throw new InvalidCastException(
CoreStrings.InvalidType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected static IEqualityComparer<object[]> CreateEqualityComparer(IReadOnlyLis
var comparers = properties.Select(p => p.GetKeyValueComparer()).ToList();

return comparers.All(c => c != null)
? new CompositeCustomComparer(comparers!)
? new CompositeCustomComparer(comparers)
: properties.Any(p => typeof(IStructuralEquatable).IsAssignableFrom(p.ClrType))
? new StructuralCompositeComparer()
: new CompositeComparer();
Expand Down
33 changes: 8 additions & 25 deletions src/EFCore/ChangeTracking/Internal/EntityReferenceMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public virtual void Update(
if (_hasSubMap
&& entityType.HasSharedClrType)
{
if (_sharedTypeReferenceMap == null)
{
_sharedTypeReferenceMap = new Dictionary<IEntityType, EntityReferenceMap>();
}
_sharedTypeReferenceMap ??= new Dictionary<IEntityType, EntityReferenceMap>();

if (!_sharedTypeReferenceMap.TryGetValue(entityType, out var sharedMap))
{
Expand Down Expand Up @@ -263,32 +260,18 @@ var numberOfStates
+ (returnDeleted ? 1 : 0)
+ (returnUnchanged ? 1 : 0);

if (numberOfStates == 1)
switch (numberOfStates)
{
if (returnUnchanged)
{
case 1 when returnUnchanged:
return _unchangedReferenceMap!.Values;
}

if (returnAdded)
{
case 1 when returnAdded:
return _addedReferenceMap!.Values;
}

if (returnModified)
{
case 1 when returnModified:
return _modifiedReferenceMap!.Values;
}

if (returnDeleted)
{
case 1 when returnDeleted:
return _deletedReferenceMap!.Values;
}
}

if (numberOfStates == 0)
{
return Enumerable.Empty<InternalEntityEntry>();
case 0:
return Enumerable.Empty<InternalEntityEntry>();
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/EFCore/ChangeTracking/Internal/IdentityMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,7 @@ private void Add(TKey key, InternalEntityEntry entry, bool updateDuplicate)
/// </summary>
public virtual IDependentsMap GetDependentsMap(IForeignKey foreignKey)
{
if (_dependentMaps == null)
{
_dependentMaps = new Dictionary<IForeignKey, IDependentsMap>(LegacyReferenceEqualityComparer.Instance);
}
_dependentMaps ??= new Dictionary<IForeignKey, IDependentsMap>(LegacyReferenceEqualityComparer.Instance);

if (!_dependentMaps.TryGetValue(foreignKey, out var map))
{
Expand Down Expand Up @@ -374,7 +371,7 @@ public virtual void Clear()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual void Remove(InternalEntityEntry entry)
=> Remove(PrincipalKeyValueFactory.CreateFromCurrentValues(entry)!, entry);
=> Remove(PrincipalKeyValueFactory.CreateFromCurrentValues(entry), entry);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -383,7 +380,7 @@ public virtual void Remove(InternalEntityEntry entry)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual void RemoveUsingRelationshipSnapshot(InternalEntityEntry entry)
=> Remove(PrincipalKeyValueFactory.CreateFromRelationshipSnapshot(entry)!, entry);
=> Remove(PrincipalKeyValueFactory.CreateFromRelationshipSnapshot(entry), entry);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
46 changes: 21 additions & 25 deletions src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,20 @@ private bool PrepareForAdd(EntityState newState)

private void SetEntityState(EntityState oldState, EntityState newState, bool acceptChanges, bool modifyProperties)
{
var entityType = EntityType;

// Prevent temp values from becoming permanent values
if (oldState == EntityState.Added
&& newState != EntityState.Added
&& newState != EntityState.Detached)
{
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var property in entityType.GetProperties())
foreach (var property in EntityType.GetProperties())
{
if (property.IsKey() && HasTemporaryValue(property))
{
throw new InvalidOperationException(
CoreStrings.TempValuePersists(
property.Name,
entityType.DisplayName(), newState));
EntityType.DisplayName(), newState));
}
}
}
Expand All @@ -257,10 +255,10 @@ private void SetEntityState(EntityState oldState, EntityState newState, bool acc
if (newState == EntityState.Modified
&& modifyProperties)
{
_stateData.FlagAllProperties(entityType.PropertyCount(), PropertyFlag.Modified, flagged: true);
_stateData.FlagAllProperties(EntityType.PropertyCount(), PropertyFlag.Modified, flagged: true);

// Hot path; do not use LINQ
foreach (var property in entityType.GetProperties())
foreach (var property in EntityType.GetProperties())
{
if (property.GetAfterSaveBehavior() != PropertySaveBehavior.Save)
{
Expand All @@ -277,7 +275,7 @@ private void SetEntityState(EntityState oldState, EntityState newState, bool acc
if (newState == EntityState.Unchanged)
{
_stateData.FlagAllProperties(
entityType.PropertyCount(), PropertyFlag.Modified,
EntityType.PropertyCount(), PropertyFlag.Modified,
flagged: false);
}

Expand Down Expand Up @@ -320,7 +318,7 @@ private void SetEntityState(EntityState oldState, EntityState newState, bool acc
|| newState == EntityState.Detached)
&& HasConceptualNull)
{
_stateData.FlagAllProperties(entityType.PropertyCount(), PropertyFlag.Null, flagged: false);
_stateData.FlagAllProperties(EntityType.PropertyCount(), PropertyFlag.Null, flagged: false);
}

if (oldState == EntityState.Detached
Expand All @@ -341,7 +339,7 @@ private void SetEntityState(EntityState oldState, EntityState newState, bool acc

FireStateChanged(oldState);

HandleSharedIdentityEntry(oldState, newState, entityType);
HandleSharedIdentityEntry(oldState, newState, EntityType);

if ((newState == EntityState.Deleted
|| newState == EntityState.Detached)
Expand Down Expand Up @@ -1309,7 +1307,7 @@ private void SetProperty(
}

EnsureStoreGeneratedValues();
_storeGeneratedValues.SetValue(asProperty!, value, storeGeneratedIndex);
_storeGeneratedValues.SetValue(asProperty, value, storeGeneratedIndex);
}
else
{
Expand Down Expand Up @@ -1426,23 +1424,21 @@ public void AcceptChanges()
_stateData.FlagAllProperties(EntityType.PropertyCount(), PropertyFlag.Unknown, false);

var currentState = EntityState;
if ((currentState == EntityState.Unchanged)
|| (currentState == EntityState.Detached))
switch (currentState)
{
return;
}

if ((currentState == EntityState.Added)
|| (currentState == EntityState.Modified))
{
_originalValues.AcceptChanges(this);
SharedIdentityEntry?.AcceptChanges();
case EntityState.Unchanged:
case EntityState.Detached:
return;
case EntityState.Added:
case EntityState.Modified:
_originalValues.AcceptChanges(this);
SharedIdentityEntry?.AcceptChanges();

SetEntityState(EntityState.Unchanged, true);
}
else if (currentState == EntityState.Deleted)
{
SetEntityState(EntityState.Detached);
SetEntityState(EntityState.Unchanged, true);
break;
case EntityState.Deleted:
SetEntityState(EntityState.Detached);
break;
}
}

Expand Down
Loading

0 comments on commit 0b166bc

Please sign in to comment.