Skip to content

Commit

Permalink
Update for expression trees
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed Apr 10, 2024
1 parent f823901 commit 0ca4619
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Analyzers/DoNotCastIQueryableImplicitlyAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ private void AnalyzeOperationAction(OperationAnalysisContext context)
return;
}

var parent = conversionOperation.Parent;
var checkedParents = new List<IOperation>();
while (parent != null) {
if (checkedParents.Contains(parent)) {
break;
}
checkedParents.Add(parent);
if (parent.Type?.Name == nameof(System.Linq.Expressions.Expression)) {
return;
}
parent = parent.Parent;
}

var diagnostic = Diagnostic.Create(
NoImplicitIQueryableCasts,
conversionOperation.Syntax.GetLocation(),
Expand Down
23 changes: 23 additions & 0 deletions src/Tests/DoNotCastIQueryableImplicitlyAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,27 @@ public TestClass()

await VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact]
public async Task IQueryable_WithinQuery()
{
const string source =
"""
using System.Linq;

public class TestClass
{
public TestClass()
{
var query = Enumerable.Empty<int>().AsQueryable();
var query2 = query.Where(x => query.ToList().Count() > 5);
}
}
"""
;

await VerifyCS.VerifyAnalyzerAsync(source);
}

// todo: check for Queryable.Single and other non-async methods on IQueryable
}
1 change: 1 addition & 0 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);IDE1006</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 0ca4619

Please sign in to comment.