Skip to content

Commit

Permalink
Fix S6605 and S6617 FP: Replace DbSet check with IQueryable<T> check (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-paidis-sonarsource authored Jun 5, 2023
1 parent d5416d1 commit e63e4bc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion analyzers/src/SonarAnalyzer.Common/Helpers/KnownType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public sealed partial class KnownType
public static readonly KnownType Microsoft_Azure_WebJobs_FunctionNameAttribute = new("Microsoft.Azure.WebJobs.FunctionNameAttribute");
public static readonly KnownType Microsoft_Data_Sqlite_SqliteCommand = new("Microsoft.Data.Sqlite.SqliteCommand");
public static readonly KnownType Microsoft_EntityFrameworkCore_DbContextOptionsBuilder = new("Microsoft.EntityFrameworkCore.DbContextOptionsBuilder");
public static readonly KnownType Microsoft_EntityFrameworkCore_DbSet_TEntity = new("Microsoft.EntityFrameworkCore.DbSet", "TEntity");
public static readonly KnownType Microsoft_EntityFrameworkCore_Migrations_Migration = new("Microsoft.EntityFrameworkCore.Migrations.Migration");
public static readonly KnownType Microsoft_EntityFrameworkCore_MySQLDbContextOptionsExtensions = new("Microsoft.EntityFrameworkCore.MySQLDbContextOptionsExtensions");
public static readonly KnownType Microsoft_EntityFrameworkCore_NpgsqlDbContextOptionsExtensions = new("Microsoft.EntityFrameworkCore.NpgsqlDbContextOptionsExtensions");
Expand Down Expand Up @@ -328,6 +327,7 @@ public sealed partial class KnownType
public static readonly KnownType System_Linq_Expressions_Expression = new("System.Linq.Expressions.Expression");
public static readonly KnownType System_Linq_Expressions_Expression_T = new("System.Linq.Expressions.Expression", "TDelegate");
public static readonly KnownType System_Linq_ImmutableArrayExtensions = new("System.Linq.ImmutableArrayExtensions");
public static readonly KnownType System_Linq_IQueryable = new("System.Linq.IQueryable", "T");
public static readonly KnownType System_Linq_Queryable = new("System.Linq.Queryable");
public static readonly KnownType System_MarshalByRefObject = new("System.MarshalByRefObject");
public static readonly KnownType System_MTAThreadAttribute = new("System.MTAThreadAttribute");
Expand Down
6 changes: 1 addition & 5 deletions analyzers/src/SonarAnalyzer.Common/Rules/InsteadOfAnyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ public abstract class InsteadOfAnyBase<TSyntaxKind, TInvocationExpression> : Son
KnownType.System_Collections_Generic_HashSet_T,
KnownType.System_Collections_Generic_SortedSet_T);

protected static readonly ImmutableArray<KnownType> DbSetTypes = ImmutableArray.Create(
KnownType.System_Data_Entity_DbSet_TEntity,
KnownType.Microsoft_EntityFrameworkCore_DbSet_TEntity);

protected abstract ILanguageFacade<TSyntaxKind> Language { get; }
protected abstract HashSet<TSyntaxKind> ExitParentKinds { get; }

Expand Down Expand Up @@ -140,7 +136,7 @@ private bool IsUsedByEntityFramework(SyntaxNode node, SemanticModel model)

if (Language.Syntax.IsKind(node, Language.SyntaxKind.InvocationExpression)
&& Language.Syntax.TryGetOperands(node, out var left, out var _)
&& model.GetTypeInfo(left).Type.DerivesFromAny(DbSetTypes))
&& model.GetTypeInfo(left).Type.DerivesOrImplements(KnownType.System_Linq_IQueryable))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public void GetEntities(FirstContext dbContext, List<int> ids)

_ = dbContext.MyEntities.Where(e => ids.Any(i => e.Id is i)); // Error [CS0150]
_ = dbContext.MyEntities.Where(e => ids.Any(i => e.Id is 2)); // Error [CS8122]

var iqueryable = dbContext.MyEntities.OrderBy(e => e.Id);
_ = iqueryable.Where(e => ids.Any(i => e.Id == i)); // Compliant
}

public async Task GetEntitiesAsync(SecondContext secondContext, List<int> ids)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Public Class EntityFrameworkTestcases
__ = dbContext.MyEntities.Where(Function(e) ids.Any(Function(i) e.Equals(i))) ' Compliant
__ = dbContext.MyEntities.Where(Function(e) ids.Any(Function(i) e.Id > i)) ' Compliant
__ = dbContext.MyEntities.Where(Function(e) ids.Any(Function(i) TypeOf e.Id Is i)) ' Error [BC30002]

Dim iqueryable = dbContext.MyEntities.OrderBy(Function(e) e.Id)
__ = iqueryable.Where(Function(e) ids.Any(Function(i) e.Id = i)) ' Compliant

End Sub
End Class
End Class
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public void GetEntities(FirstContext dbContext, List<int> ids)
_ = dbContext.MyEntities.Where(e => ids.Any(i => e.Id == i)); // Compliant
_ = dbContext.MyEntities.Where(e => ids.Any(i => e.Equals(i))); // Compliant
_ = dbContext.MyEntities.Where(e => ids.Any(i => e.Id > i)); // Compliant

var iqueryable = dbContext.MyEntities.OrderBy(e => e.Id);
_ = iqueryable.Where(e => ids.Any(i => e.Id == i)); // Compliant
}

public async Task GetEntitiesAsync(SecondContext secondContext, List<int> ids)
Expand Down

0 comments on commit e63e4bc

Please sign in to comment.