Skip to content

Commit

Permalink
Enable internal code usage analyzer on the providers
Browse files Browse the repository at this point in the history
Fixes #15393
  • Loading branch information
ajcvickers committed Apr 7, 2020
1 parent 2215c3b commit a24136a
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 94 deletions.
1 change: 1 addition & 0 deletions src/EFCore.Cosmos/EFCore.Cosmos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<ProjectReference Include="..\EFCore\EFCore.csproj" PrivateAssets="contentfiles;build" />
<ProjectReference Condition="'$(DisableEFAnalyzer)' != 'true'" Include="..\EFCore.Analyzers\EFCore.Analyzers.csproj" ReferenceOutputAssembly="False" OutputItemType="Analyzer" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -366,9 +365,12 @@ private void AddInclude(
var includeMethod = navigation.IsCollection ? _includeCollectionMethodInfo : _includeReferenceMethodInfo;
var includingClrType = navigation.DeclaringEntityType.ClrType;
var relatedEntityClrType = navigation.TargetEntityType.ClrType;
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var entityEntryVariable = _trackQueryResults
? shaperBlock.Variables.Single(v => v.Type == typeof(InternalEntityEntry))
: (Expression)Expression.Constant(null, typeof(InternalEntityEntry));
#pragma warning restore EF1001 // Internal EF Core API usage.
var concreteEntityTypeVariable = shaperBlock.Variables.Single(v => v.Type == typeof(IEntityType));
var inverseNavigation = navigation.Inverse;
var fixup = GenerateFixup(
Expand All @@ -395,7 +397,10 @@ private static readonly MethodInfo _includeReferenceMethodInfo
.GetDeclaredMethod(nameof(IncludeReference));

private static void IncludeReference<TIncludingEntity, TIncludedEntity>(
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
InternalEntityEntry entry,
#pragma warning restore EF1001 // Internal EF Core API usage.
object entity,
IEntityType entityType,
TIncludedEntity relatedEntity,
Expand Down Expand Up @@ -427,7 +432,10 @@ private static void IncludeReference<TIncludingEntity, TIncludedEntity>(
// For non-null relatedEntity StateManager will set the flag
else if (relatedEntity == null)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
entry.SetIsLoaded(navigation);
#pragma warning restore EF1001 // Internal EF Core API usage.
}
}

Expand All @@ -436,7 +444,10 @@ private static readonly MethodInfo _includeCollectionMethodInfo
.GetDeclaredMethod(nameof(IncludeCollection));

private static void IncludeCollection<TIncludingEntity, TIncludedEntity>(
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
InternalEntityEntry entry,
#pragma warning restore EF1001 // Internal EF Core API usage.
object entity,
IEntityType entityType,
IEnumerable<TIncludedEntity> relatedEntities,
Expand Down Expand Up @@ -474,7 +485,10 @@ private static void IncludeCollection<TIncludingEntity, TIncludedEntity>(
}
else
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
entry.SetIsLoaded(navigation);
#pragma warning restore EF1001 // Internal EF Core API usage.
if (relatedEntities != null)
{
using var enumerator = relatedEntities.GetEnumerator();
Expand Down
12 changes: 12 additions & 0 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,18 @@ public override int SaveChanges(IList<IUpdateEntry> entries)

if (!entityType.IsDocumentRoot())
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var root = GetRootDocument((InternalEntityEntry)entry);
#pragma warning restore EF1001 // Internal EF Core API usage.
if (!entriesSaved.Contains(root)
&& rootEntriesToSave.Add(root)
&& root.EntityState == EntityState.Unchanged)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
((InternalEntityEntry)root).SetEntityState(EntityState.Modified);
#pragma warning restore EF1001 // Internal EF Core API usage.
}

continue;
Expand Down Expand Up @@ -154,7 +160,10 @@ public override async Task<int> SaveChangesAsync(
&& rootEntriesToSave.Add(root)
&& root.EntityState == EntityState.Unchanged)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
((InternalEntityEntry)root).SetEntityState(EntityState.Modified);
#pragma warning restore EF1001 // Internal EF Core API usage.
}

continue;
Expand Down Expand Up @@ -325,6 +334,8 @@ public virtual DocumentSource GetDocumentSource([NotNull] IEntityType entityType
return documentSource;
}

#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
private IUpdateEntry GetRootDocument(InternalEntityEntry entry)
{
var stateManager = entry.StateManager;
Expand All @@ -349,6 +360,7 @@ private IUpdateEntry GetRootDocument(InternalEntityEntry entry)

return principal.EntityType.IsDocumentRoot() ? principal : GetRootDocument(principal);
}
#pragma warning restore EF1001 // Internal EF Core API usage.

private Exception ThrowUpdateException(CosmosException exception, IUpdateEntry entry)
{
Expand Down
23 changes: 22 additions & 1 deletion src/EFCore.Cosmos/Update/Internal/DocumentSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,23 @@ public virtual JObject CreateDocument([NotNull] IUpdateEntry entry, int? ordinal
}
else if (fk.IsUnique)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var dependentEntry = ((InternalEntityEntry)entry).StateManager.TryGetEntry(embeddedValue, fk.DeclaringEntityType);
document[embeddedPropertyName] = _database.GetDocumentSource(dependentEntry.EntityType).CreateDocument(dependentEntry);
#pragma warning restore EF1001 // Internal EF Core API usage.
}
else
{
var embeddedOrdinal = 0;
var array = new JArray();
foreach (var dependent in (IEnumerable)embeddedValue)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var dependentEntry = ((InternalEntityEntry)entry).StateManager.TryGetEntry(dependent, fk.DeclaringEntityType);
array.Add(_database.GetDocumentSource(dependentEntry.EntityType).CreateDocument(dependentEntry, embeddedOrdinal));
#pragma warning restore EF1001 // Internal EF Core API usage.
embeddedOrdinal++;
}

Expand All @@ -141,7 +147,7 @@ public virtual JObject CreateDocument([NotNull] IUpdateEntry entry, int? ordinal
/// </summary>
public virtual JObject UpdateDocument([NotNull] JObject document, [NotNull] IUpdateEntry entry)
=> UpdateDocument(document, entry, null);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand All @@ -151,7 +157,10 @@ public virtual JObject UpdateDocument([NotNull] JObject document, [NotNull] IUpd
public virtual JObject UpdateDocument([NotNull] JObject document, [NotNull] IUpdateEntry entry, int? ordinal)
{
var anyPropertyUpdated = false;
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var stateManager = ((InternalEntityEntry)entry).StateManager;
#pragma warning restore EF1001 // Internal EF Core API usage.
foreach (var property in entry.EntityType.GetProperties())
{
if (entry.EntityState == EntityState.Added
Expand Down Expand Up @@ -196,7 +205,10 @@ public virtual JObject UpdateDocument([NotNull] JObject document, [NotNull] IUpd
}
else if (fk.IsUnique)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var embeddedEntry = ((InternalEntityEntry)entry).StateManager.TryGetEntry(embeddedValue, fk.DeclaringEntityType);
#pragma warning restore EF1001 // Internal EF Core API usage.
if (embeddedEntry == null)
{
continue;
Expand All @@ -222,6 +234,8 @@ public virtual JObject UpdateDocument([NotNull] JObject document, [NotNull] IUpd
var shouldSetTemporaryKeys = false;
foreach (var dependent in (IEnumerable)embeddedValue)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var embeddedEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType);
if (embeddedEntry == null)
{
Expand All @@ -233,6 +247,7 @@ public virtual JObject UpdateDocument([NotNull] JObject document, [NotNull] IUpd
shouldSetTemporaryKeys = true;
break;
}
#pragma warning restore EF1001 // Internal EF Core API usage.

embeddedOrdinal++;
}
Expand All @@ -242,13 +257,16 @@ public virtual JObject UpdateDocument([NotNull] JObject document, [NotNull] IUpd
var temporaryOrdinal = -1;
foreach (var dependent in (IEnumerable)embeddedValue)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var embeddedEntry = stateManager.TryGetEntry(dependent, fk.DeclaringEntityType);
if (embeddedEntry == null)
{
continue;
}

embeddedEntry.SetTemporaryValue(ordinalKeyProperty, temporaryOrdinal, setModified: false);
#pragma warning restore EF1001 // Internal EF Core API usage.

temporaryOrdinal--;
}
Expand All @@ -259,7 +277,10 @@ public virtual JObject UpdateDocument([NotNull] JObject document, [NotNull] IUpd
var array = new JArray();
foreach (var dependent in (IEnumerable)embeddedValue)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
// #16707
var embeddedEntry = ((InternalEntityEntry)entry).StateManager.TryGetEntry(dependent, fk.DeclaringEntityType);
#pragma warning restore EF1001 // Internal EF Core API usage.
if (embeddedEntry == null)
{
continue;
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.InMemory/EFCore.InMemory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<ProjectReference Include="..\EFCore\EFCore.csproj" PrivateAssets="contentfiles;build" />
<ProjectReference Condition="'$(DisableEFAnalyzer)' != 'true'" Include="..\EFCore.Analyzers\EFCore.Analyzers.csproj" ReferenceOutputAssembly="False" OutputItemType="Analyzer" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,10 @@ private static (Expression, Expression) DecomposeJoinCondition(Expression joinCo

static Expression CreateAnonymousObject(List<Expression> expressions)
=> Expression.New(
#pragma warning disable EF1001 // Internal EF Core API usage.
// #20565
AnonymousObject.AnonymousObjectCtor,
#pragma warning restore EF1001 // Internal EF Core API usage.
Expression.NewArrayInit(
typeof(object),
expressions.Select(e => Expression.Convert(e, typeof(object)))));
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.Proxies/EFCore.Proxies.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<ItemGroup>
<ProjectReference Include="..\EFCore\EFCore.csproj" PrivateAssets="contentfiles;build" />
<ProjectReference Condition="'$(DisableEFAnalyzer)' != 'true'" Include="..\EFCore.Analyzers\EFCore.Analyzers.csproj" ReferenceOutputAssembly="False" OutputItemType="Analyzer" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore.Proxies/Proxies/Internal/ProxyBindingRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,18 @@ public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder,
var proxyType = _proxyFactory.CreateProxyType(_options, entityType);

// WARNING: This code is EF internal; it should not be copied. See #10789 #14554
#pragma warning disable EF1001 // Internal EF Core API usage.
var binding = (InstantiationBinding)entityType[CoreAnnotationNames.ConstructorBinding];
#pragma warning restore EF1001 // Internal EF Core API usage.
if (binding == null)
{
_directBindingConvention.ProcessModelFinalizing(modelBuilder, context);
}

// WARNING: This code is EF internal; it should not be copied. See #10789 #14554
#pragma warning disable EF1001 // Internal EF Core API usage.
binding = (InstantiationBinding)entityType[CoreAnnotationNames.ConstructorBinding];
#pragma warning restore EF1001 // Internal EF Core API usage.

if (_options.UseLazyLoadingProxies)
{
Expand All @@ -106,7 +110,9 @@ public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder,

entityType.SetAnnotation(
// WARNING: This code is EF internal; it should not be copied. See #10789 #14554
#pragma warning disable EF1001 // Internal EF Core API usage.
CoreAnnotationNames.ConstructorBinding,
#pragma warning restore EF1001 // Internal EF Core API usage.
new FactoryMethodBinding(
_proxyFactory,
_createLazyLoadingProxyMethod,
Expand All @@ -123,7 +129,9 @@ public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder,
{
entityType.SetAnnotation(
// WARNING: This code is EF internal; it should not be copied. See #10789 #14554
#pragma warning disable EF1001 // Internal EF Core API usage.
CoreAnnotationNames.ConstructorBinding,
#pragma warning restore EF1001 // Internal EF Core API usage.
new FactoryMethodBinding(
_proxyFactory,
_createProxyMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public virtual void ProcessModelInitialized(IConventionModelBuilder modelBuilder
if (_options?.UseChangeTrackingProxies == true)
{
modelBuilder.HasChangeTrackingStrategy(ChangeTrackingStrategy.ChangingAndChangedNotifications);
#pragma warning disable EF1001 // Internal EF Core API usage.
modelBuilder.HasAnnotation(CoreAnnotationNames.SkipChangeTrackingStrategyValidationAnnotation, "true");
#pragma warning restore EF1001 // Internal EF Core API usage.
}
}
}
Expand Down

This file was deleted.

6 changes: 6 additions & 0 deletions src/EFCore.Relational/Query/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq.Expressions;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
Expand All @@ -10,6 +11,11 @@ namespace Microsoft.EntityFrameworkCore.Query
{
public static class ExpressionExtensions
{
public static bool IsLogicalNot([NotNull] this SqlUnaryExpression sqlUnaryExpression)
=> sqlUnaryExpression.OperatorType == ExpressionType.Not
&& (sqlUnaryExpression.Type == typeof(bool)
|| sqlUnaryExpression.Type == typeof(bool?));

public static RelationalTypeMapping InferTypeMapping([NotNull] params SqlExpression[] expressions)
{
Check.NotNull(expressions, nameof(expressions));
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.SqlServer.NTS/EFCore.SqlServer.NTS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<ItemGroup>
<ProjectReference Include="..\EFCore.SqlServer\EFCore.SqlServer.csproj" PrivateAssets="contentfiles;build" />
<ProjectReference Condition="'$(DisableEFAnalyzer)' != 'true'" Include="..\EFCore.Analyzers\EFCore.Analyzers.csproj" ReferenceOutputAssembly="False" OutputItemType="Analyzer" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.SqlServer/EFCore.SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<ProjectReference Include="..\EFCore.Relational\EFCore.Relational.csproj" PrivateAssets="contentfiles;build" />
<ProjectReference Condition="'$(DisableEFAnalyzer)' != 'true'" Include="..\EFCore.Analyzers\EFCore.Analyzers.csproj" ReferenceOutputAssembly="False" OutputItemType="Analyzer" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.SqlServer.Internal;
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.Sqlite.Core/EFCore.Sqlite.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<ItemGroup>
<ProjectReference Include="..\EFCore.Relational\EFCore.Relational.csproj" PrivateAssets="contentfiles;build" />
<ProjectReference Include="..\Microsoft.Data.Sqlite.Core\Microsoft.Data.Sqlite.Core.csproj" />
<ProjectReference Condition="'$(DisableEFAnalyzer)' != 'true'" Include="..\EFCore.Analyzers\EFCore.Analyzers.csproj" ReferenceOutputAssembly="False" OutputItemType="Analyzer" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit a24136a

Please sign in to comment.