Skip to content

Commit 816dae1

Browse files
committed
Delete IsFromDbSet for subquery (#72)
1 parent 621f2d0 commit 816dae1

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

src/Kuker.Analyzers/Rules/Kuk0005TagWithCallSiteOnExecutionAnalyzer.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,31 +269,36 @@ CompilationSymbolsModel model
269269
{
270270
return false;
271271
}
272+
272273
IMethodSymbol methodSymbol = GetInvocationMethodSymbol(invocation, context);
274+
273275
if (methodSymbol == null)
274276
{
275277
return false;
276278
}
279+
277280
IMethodSymbol originalMethod = methodSymbol.ReducedFrom ?? methodSymbol;
281+
278282
if (!s_queryLambdaMethodNames.Contains(originalMethod.Name))
279283
{
280284
return false;
281285
}
286+
282287
if (!IsQueryableOperatorMethod(originalMethod, model))
283288
{
284289
return false;
285290
}
291+
286292
ExpressionSyntax sourceExpression = GetQuerySourceExpression(invocation, originalMethod);
293+
287294
if (sourceExpression == null)
288295
{
289296
return false;
290297
}
298+
291299
ITypeSymbol sourceType = context.SemanticModel.GetTypeInfo(sourceExpression).Type;
292-
if (!ImplementsIQueryable(sourceType, model))
293-
{
294-
return false;
295-
}
296-
return IsFromDbSet(sourceExpression, context, model);
300+
301+
return ImplementsIQueryable(sourceType, model);
297302
}
298303

299304
private static IMethodSymbol GetInvocationMethodSymbol(

tests/Kuker.Analyzers.Tests/Rules/Kuk0005TagWithCallSiteOnExecutionAnalyzerTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,52 @@ select c
427427
_appDbContext.Users.Any(u => _appDbContext.Companies.Any(c => c.Id == u.Id) && u.Id == id)
428428
).ToArray();
429429
""", 14, 5, 14, 95)]
430+
[InlineData("""
431+
// Execution inside execute (subquery) OK
432+
IQueryable<User> baseQuery = GetUserQuery();
433+
434+
UserSummary[] result = await baseQuery.TagWithCallSite()
435+
.Select(u => new UserSummary
436+
{
437+
Id = u.Id,
438+
Name = u.Name,
439+
440+
CompanyName = _appDbContext.Companies
441+
.Where(c => c.Id == u.CompanyId)
442+
.Select(c => c.Name)
443+
.FirstOrDefault(),
444+
445+
RelatedUsers = _appDbContext.Users
446+
.Where(x => x.CompanyId == u.CompanyId)
447+
.OrderBy(x => x.Name)
448+
.Select(x => x.Name)
449+
.ToArray(),
450+
})
451+
.ToArrayAsync();
452+
""", 0, 0, 0, 0)]
453+
[InlineData("""
454+
// Execution inside execute (subquery) violation
455+
IQueryable<User> baseQuery = GetUserQuery();
456+
457+
UserSummary[] result = await baseQuery
458+
.Select(u => new UserSummary
459+
{
460+
Id = u.Id,
461+
Name = u.Name,
462+
463+
CompanyName = _appDbContext.Companies
464+
.Where(c => c.Id == u.CompanyId)
465+
.Select(c => c.Name)
466+
.FirstOrDefault(),
467+
468+
RelatedUsers = _appDbContext.Users
469+
.Where(x => x.CompanyId == u.CompanyId)
470+
.OrderBy(x => x.Name)
471+
.Select(x => x.Name)
472+
.ToArray(),
473+
})
474+
.ToArrayAsync();
475+
""", 16, 30, 33, 20)]
430476
#pragma warning restore RCS0053,SA1117 // Fix formatting of a list
431477
public async Task RunAsync(string inputQuery, int startLine, int startColumn, int endLine, int endColumn)
432478
{
@@ -483,6 +529,17 @@ public class Company
483529
public long Id { get; set; }
484530
public string Name { get; set; }
485531
}
532+
533+
public class UserSummary
534+
{
535+
public long Id { get; set; }
536+
537+
public string Name { get; set; }
538+
539+
public string CompanyName { get; set; }
540+
541+
public string[] RelatedUsers { get; set; }
542+
}
486543
487544
public class AppDbContext : DbContext
488545
{

0 commit comments

Comments
 (0)