Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S6966 FP: EntityFrameworks DbContext/DBSet Add/AddRange methods are preferred over their Async counterpart #9318

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public sealed class UseAwaitableMethod : SonarDiagnosticAnalyzer
{
private const string DiagnosticId = "S6966";
private const string MessageFormat = "Await {0} instead.";
private static readonly string[] ExcludedMethodNamesAddAddRange = ["Add", "AddRange"];

private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat);

Expand Down Expand Up @@ -93,7 +94,8 @@ private static ImmutableArray<ISymbol> FindAwaitableAlternatives(WellKnownExtens
&& invocationExpression.EnclosingScope() is { } scope
&& IsAsyncCodeBlock(scope)
&& semanticModel.GetSymbolInfo(invocationExpression, cancel).Symbol is IMethodSymbol { MethodKind: not MethodKind.DelegateInvoke } methodSymbol
&& !methodSymbol.IsAwaitableNonDynamic()) // The invoked method returns something awaitable (but it isn't awaited).
&& !methodSymbol.IsAwaitableNonDynamic() // The invoked method returns something awaitable (but it isn't awaited).
&& !IsExcluded(methodSymbol))
{
// Perf: Before doing (expensive) speculative re-binding in SpeculativeBindCandidates, we check if there is an "..Async()" alternative in scope.
var invokedType = invocationExpression.Expression.GetLeftOfDot() is { } expression && semanticModel.GetTypeInfo(expression) is { Type: { } type }
Expand All @@ -110,6 +112,10 @@ private static ImmutableArray<ISymbol> FindAwaitableAlternatives(WellKnownExtens
return ImmutableArray<ISymbol>.Empty;
}

private static bool IsExcluded(IMethodSymbol methodSymbol) =>
methodSymbol.IsAny(KnownType.Microsoft_EntityFrameworkCore_DbSet_TEntity, ExcludedMethodNamesAddAddRange) // https://github.com/SonarSource/sonar-dotnet/issues/9269
|| methodSymbol.IsAny(KnownType.Microsoft_EntityFrameworkCore_DbContext, ExcludedMethodNamesAddAddRange); // https://github.com/SonarSource/sonar-dotnet/issues/9269

private static IEnumerable<IMethodSymbol> GetMethodSymbolsInScope(string methodName, WellKnownExtensionMethodContainer wellKnownExtensionMethodContainer,
ITypeSymbol invokedType, ITypeSymbol methodContainer) =>
((ITypeSymbol[])[.. invokedType.GetSelfAndBaseTypes(), .. WellKnownExtensionMethodContainer(wellKnownExtensionMethodContainer, methodContainer), methodContainer])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@
using System.Linq;
using System.Collections.Generic;

public class EnitityFramework
public class EntityFramework
{
public async Task Query()
{
DbSet<object> dbSet = default;
dbSet.Add(null); // Noncompliant FP https://github.com/SonarSource/sonar-dotnet/issues/9269
dbSet.AddRange(null); // Noncompliant FP https://github.com/SonarSource/sonar-dotnet/issues/9269
dbSet.All(x => true); // Noncompliant
dbSet.Any(x => true); // Noncompliant
dbSet.Average(x => 1); // Noncompliant
dbSet.Contains(null); // Noncompliant
dbSet.Count(); // Noncompliant
dbSet.ElementAt(1); // Compliant: EF 8.0 only
dbSet.Add(null); // Compliant https://github.com/SonarSource/sonar-dotnet/issues/9269
dbSet.AddRange(null); // Compliant https://github.com/SonarSource/sonar-dotnet/issues/9269
dbSet.All(x => true); // Noncompliant
dbSet.Any(x => true); // Noncompliant
dbSet.Average(x => 1); // Noncompliant
dbSet.Contains(null); // Noncompliant
dbSet.Count(); // Noncompliant
dbSet.ElementAt(1); // Compliant: EF 8.0 only
dbSet.ElementAtOrDefault(1); // Compliant: EF 8.0 only
dbSet.ExecuteDelete(); // Noncompliant
dbSet.ExecuteDelete(); // Noncompliant
dbSet.ExecuteUpdate(x => x.SetProperty(x => x.ToString(), x => string.Empty)); // Noncompliant
dbSet.Find(null); // Noncompliant
dbSet.First(); // Noncompliant
dbSet.FirstOrDefault(); // Noncompliant
dbSet.Last(); // Noncompliant
dbSet.LastOrDefault(); // Noncompliant
dbSet.Load(); // Noncompliant
dbSet.LongCount(); // Noncompliant
dbSet.Max(); // Noncompliant
dbSet.Min(); // Noncompliant
dbSet.Single(); // Noncompliant
dbSet.SingleOrDefault(); // Noncompliant
dbSet.Sum(x => 0); // Noncompliant
dbSet.ToArray(); // Noncompliant
dbSet.ToDictionary(x => 0); // Noncompliant
dbSet.ToList(); // Noncompliant
dbSet.Find(null); // Noncompliant
dbSet.First(); // Noncompliant
dbSet.FirstOrDefault(); // Noncompliant
dbSet.Last(); // Noncompliant
dbSet.LastOrDefault(); // Noncompliant
dbSet.Load(); // Noncompliant
dbSet.LongCount(); // Noncompliant
dbSet.Max(); // Noncompliant
dbSet.Min(); // Noncompliant
dbSet.Single(); // Noncompliant
dbSet.SingleOrDefault(); // Noncompliant
dbSet.Sum(x => 0); // Noncompliant
dbSet.ToArray(); // Noncompliant
dbSet.ToDictionary(x => 0); // Noncompliant
dbSet.ToList(); // Noncompliant
}

public async Task NotIQueryable(IEnumerable<object> enumerable)
Expand All @@ -48,8 +48,8 @@ public async Task NotIQueryable(IEnumerable<object> enumerable)

public async Task Context(DbContext dbContext)
{
dbContext.Add(null); // Noncompliant FP https://github.com/SonarSource/sonar-dotnet/issues/9269
dbContext.AddRange(null); // Noncompliant FP https://github.com/SonarSource/sonar-dotnet/issues/9269
dbContext.Add(null); // Compliant https://github.com/SonarSource/sonar-dotnet/issues/9269
dbContext.AddRange(null); // Compliant https://github.com/SonarSource/sonar-dotnet/issues/9269
dbContext.Dispose(); // Noncompliant
dbContext.Find<object>(); // Noncompliant
dbContext.Find(typeof(object)); // Noncompliant
Expand Down
Loading