Skip to content

Commit

Permalink
Don´t emit CA1849 when using DbSet.Add and DbSet.AddRange EntityFrame…
Browse files Browse the repository at this point in the history
…work Methods (#6858)

* ignore DbSet.Add and DbSet.AddRange for CA1849 as well

* fix typo

* DbSet Type is generic

---------

Co-authored-by: Timo Witte <[email protected]>
  • Loading branch information
Spacefish and timo-witte authored Aug 17, 2023
1 parent 28fbf11 commit 47c7281
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,24 @@ private static void GetSymbolAndAddToList(string symbolName, string metadataName

private static ImmutableArray<IMethodSymbol> GetExcludedMethods(WellKnownTypeProvider wellKnownTypeProvider)
{
var entityFrameworkTypeNames = new[]
{
WellKnownTypeNames.MicrosoftEntityFrameworkCoreDbContext,
WellKnownTypeNames.MicrosoftEntityFrameworkCoreDbSet1
};

var methodsBuilder = ImmutableArray.CreateBuilder<IMethodSymbol>();
if (wellKnownTypeProvider.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftEntityFrameworkCoreDbContext, out INamedTypeSymbol? dbContextType))

foreach (var entityFrameworkTypeName in entityFrameworkTypeNames)
{
foreach (var method in dbContextType.GetMembers().OfType<IMethodSymbol>())
if (wellKnownTypeProvider.TryGetOrCreateTypeByMetadataName(entityFrameworkTypeName, out INamedTypeSymbol? entityFrameworkType))
{
if (method.Name is "Add" or "AddRange")
foreach (var method in entityFrameworkType.GetMembers().OfType<IMethodSymbol>())
{
methodsBuilder.Add(method);
if (method.Name is "Add" or "AddRange")
{
methodsBuilder.Add(method);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,24 @@ public async Task RunAsync(DbContext ctx) {
}.RunAsync();
}

[Fact]
public Task DbSetAddRange_NoDiagnostic()
{
return new VerifyCS.Test
{
TestCode = @"
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
class Test {
public async Task RunAsync(DbSet<object> set) {
set.AddRange(1, 2);
}
}",
ReferenceAssemblies = ReferenceAssemblies.Net.Net70.WithPackages(EntityFrameworkPackages)
}.RunAsync();
}

[Fact]
[WorkItem(6684, "https://github.com/dotnet/roslyn-analyzers/issues/6684")]
public Task DbContextFactoryCreateDbContext_Diagnostic()
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/Compiler/WellKnownTypeNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ internal static class WellKnownTypeNames
public const string MicrosoftCodeAnalysisVisualBasicVisualBasicCompilation = "Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation";
public const string MicrosoftCodeAnalysisVisualBasicVisualBasicExtensions = "Microsoft.CodeAnalysis.VisualBasic.VisualBasicExtensions";
public const string MicrosoftEntityFrameworkCoreDbContext = "Microsoft.EntityFrameworkCore.DbContext";
public const string MicrosoftEntityFrameworkCoreDbSet1 = "Microsoft.EntityFrameworkCore.DbSet`1";
public const string MicrosoftEntityFrameworkCoreEntityFrameworkQueryableExtensions = "Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions";
public const string MicrosoftEntityFrameworkCoreRelationalQueryableExtensions = "Microsoft.EntityFrameworkCore.RelationalQueryableExtensions";
public const string MicrosoftExtensionsLoggingILogger = "Microsoft.Extensions.Logging.ILogger";
Expand Down

0 comments on commit 47c7281

Please sign in to comment.