From 47c7281a7f5d6e2aaff186499d2194fdb86adb4d Mon Sep 17 00:00:00 2001 From: Spacefish Date: Thu, 17 Aug 2023 20:08:18 +0200 Subject: [PATCH] =?UTF-8?q?Don=C2=B4t=20emit=20CA1849=20when=20using=20DbS?= =?UTF-8?q?et.Add=20and=20DbSet.AddRange=20EntityFramework=20Methods=20(#6?= =?UTF-8?q?858)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ignore DbSet.Add and DbSet.AddRange for CA1849 as well * fix typo * DbSet Type is generic --------- Co-authored-by: Timo Witte --- .../Runtime/UseAsyncMethodInAsyncContext.cs | 18 ++++++++++++++---- .../UseAsyncMethodInAsyncContextTests.cs | 18 ++++++++++++++++++ src/Utilities/Compiler/WellKnownTypeNames.cs | 1 + 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContext.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContext.cs index 10daf2aa98..5a575c277e 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContext.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContext.cs @@ -188,14 +188,24 @@ private static void GetSymbolAndAddToList(string symbolName, string metadataName private static ImmutableArray GetExcludedMethods(WellKnownTypeProvider wellKnownTypeProvider) { + var entityFrameworkTypeNames = new[] + { + WellKnownTypeNames.MicrosoftEntityFrameworkCoreDbContext, + WellKnownTypeNames.MicrosoftEntityFrameworkCoreDbSet1 + }; + var methodsBuilder = ImmutableArray.CreateBuilder(); - if (wellKnownTypeProvider.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftEntityFrameworkCoreDbContext, out INamedTypeSymbol? dbContextType)) + + foreach (var entityFrameworkTypeName in entityFrameworkTypeNames) { - foreach (var method in dbContextType.GetMembers().OfType()) + if (wellKnownTypeProvider.TryGetOrCreateTypeByMetadataName(entityFrameworkTypeName, out INamedTypeSymbol? entityFrameworkType)) { - if (method.Name is "Add" or "AddRange") + foreach (var method in entityFrameworkType.GetMembers().OfType()) { - methodsBuilder.Add(method); + if (method.Name is "Add" or "AddRange") + { + methodsBuilder.Add(method); + } } } } diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContextTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContextTests.cs index efa49d7ee1..e9db9ffbcf 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContextTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContextTests.cs @@ -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 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() diff --git a/src/Utilities/Compiler/WellKnownTypeNames.cs b/src/Utilities/Compiler/WellKnownTypeNames.cs index 98c4ef1ff8..91f78ab145 100644 --- a/src/Utilities/Compiler/WellKnownTypeNames.cs +++ b/src/Utilities/Compiler/WellKnownTypeNames.cs @@ -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";