Skip to content

feat: CMTK0009 reports a delegate condition that reads a whole query - #137

Merged
CaffeinatedCoder merged 2 commits into
release/2.3.0from
feat/cmtk0009-query-delegate
Oct 29, 2026
Merged

CaffeinatedCoder merged 2 commits into
release/2.3.0from
feat/cmtk0009-query-delegate

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #129

Problem

FirstOrNone, SingleOrNone and FirstOrError on an IQueryable<T> bind to the query's overload only when the condition is a lambda written at the call. A Func<T, bool> in a variable, a field or a property, a method group, an anonymous method or a call that returns a Func binds to the IEnumerable<T> overload instead. EF Core then reads every row and runs the condition in memory, and nothing warned. QueryOptionTests pinned the behaviour, and the docs said "without a warning".

Change

  • DefaultProducingCallAnalyzer: a second CMTK0009 descriptor (same id, same Warning severity, decision 34) with its own message: 'FirstOrNone' on the query binds to its IEnumerable<T> overload, since the condition is a 'Func<Shipment, bool>' and not an expression: it reads every row and runs the condition in memory. .... The sequence overloads that take a Func are resolved by symbol from TypeKit's own Option/Result classes (TypeKitSymbols.ResultClassMethods is new, beside OptionClassMethods). The receiver, after the compiler's implicit conversion is unwrapped, counts as a query when it is IQueryable<T>, implements it, or is a type parameter constrained to it. An explicit cast and AsEnumerable() stay silent.
  • Siblings found by variant analysis: LastOrNone and LastOrError given a delegate on a query read every row the same way, while their query overloads refuse to compile. They are reported too, with advice to order the other way and take the first.
  • No code fix. Ruled out in the issue comment: the condition's rewrite is the author's. A method group wrapped in a lambda would throw in EF instead, and AsEnumerable() only silences the warning.
  • Docs: a new section in the Analyzers README, "A condition that reads every row", under CMTK0009, with a compiled sample (Samples/Analyzers/QueryConditionSamples.cs), and the rule table row. The EF README, the base README, docs/option.md and docs/ef-core.md no longer say "without a warning". QueryOptionTests' summary names the rule. design/analyzers.md's row, the roadmap item removed from "Gaps, in any 2.x", a changelog line, and claims Q01–Q05 in test/oracles/claims.json.

Tests

  • QueryDelegateConditionTests: exact messages for all five lookups, the silent forms, EF Core's DbSet<T> and Set<T>(). A sweep over the new catalogue Variants/QueryDelegateShapes crosses 22 receivers (how a query is typed, and the TypeArgumentSites call shapes that move the receiver: null-conditional, the lowered static form, in a lambda, against sequences that are no query) with 17 conditions (12 delegate forms, 5 lambda or expression forms) and every lookup. A second sweep runs every TypeArgumentKinds element type. 104 cases in all.
  • Seeded and caught, each undone by the inverse edit: no query check (the src build fails, and a narrower seed that takes a List<T> for a query fails the silent test), no type-parameter bound (2 rows), no unwrap of the implicit conversion (95), explicit casts unwrapped too (the cast row), no AllInterfaces (95), and the lowered static form's receiver dropped (3).
  • The full solution passes locally (19055 tests, 471 skipped).

🤖 Generated with Claude Code

FirstOrNone, SingleOrNone and FirstOrError on an IQueryable<T> take the
condition as an Expression. A Func variable, field or property, a method
group, an anonymous method or a call that returns a Func binds the call
to the IEnumerable<T> overload, which reads every row and runs the
condition in memory. CMTK0009 (still a Warning, decision 34) now reports
it with a message of its own that names the overload it fell to.

A lambda at the call, an Expression variable or field, and a receiver
typed as a sequence (AsEnumerable(), a cast, an IEnumerable<T> local)
stay silent. The receiver is a query when it is IQueryable<T>, implements
it, or is a type parameter constrained to it, through any bound.

Variant analysis found two siblings: LastOrNone and LastOrError given a
delegate on a query bind to the sequence overload the same way, while
their query overloads refuse to compile. Both are reported, with advice
to order the other way and take the first.

The new catalogue Variants/QueryDelegateShapes holds the receivers (how
a query is typed, and the call shapes of TypeArgumentSites that move the
receiver: null-conditional, lowered static form, in a lambda) against
every condition and lookup. QueryDelegateConditionTests sweeps it, and
TypeArgumentKinds for the element type, and EF Core's DbSet. Six seeds
are each caught: no query check, no type-parameter bound, no unwrap of
the implicit conversion, explicit casts unwrapped too, no AllInterfaces,
and the lowered form's receiver dropped.

No code fix: the condition's rewrite is the author's (issue comment).

Closes #129

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 2.3.0 milestone Oct 29, 2026
@CaffeinatedCoder CaffeinatedCoder added enhancement New feature or request package/analyzers Affects CodoMetis.TypeKit.Analyzers package/efcore Affects CodoMetis.TypeKit.EntityFrameworkCore area/option-result Option, Result and their extensions labels Oct 29, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 4b4f0c6 into release/2.3.0 Oct 29, 2026
8 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/cmtk0009-query-delegate branch October 29, 2026 13:38
CaffeinatedCoder added a commit that referenced this pull request Oct 29, 2026
Both changelog lines kept: #137's CMTK0009 entry and the docs site's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/option-result Option, Result and their extensions enhancement New feature or request package/analyzers Affects CodoMetis.TypeKit.Analyzers package/efcore Affects CodoMetis.TypeKit.EntityFrameworkCore

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant