Do not report MA0078 when the selector does more than casting its element - #1473
Merged
Merged
Conversation
…ment The analyzer used the first return operation and the first lambda found anywhere under the selector argument, without checking that the cast was the whole selector body. The code fix then replaced Select with Cast<T>(), silently removing other statements (and their side effects), ignoring other return paths, or matching a lambda that was not the selector itself (e.g. Select(Wrap(x => (T)x))). The selector must now be a lambda passed directly as the argument whose body is a single return of a cast of its first parameter.
This was referenced Sep 12, 2026
This was referenced Sep 17, 2026
Merged
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MA0078 took the first
returnoperation and the first lambda found anywhere under theSelectselector argument, without checking that the cast was the whole selector body. The code fix then replacedSelectwithCast<T>(), which could change behavior:The same logic also reported:
dt => { if (dt.Name is not null) return (BaseType)dt; return null; }source.Select(Wrap(dt => (BaseType)dt))Fix
The selector must now be a lambda passed directly as the argument (
IDelegateCreationOperationtargeting anIAnonymousFunctionOperation), whose body is exactly onereturnof a cast of its first parameter. The existing checks (noas, no numeric/user-defined conversions, enum handling) are unchanged.x => (T)xandx => { return (T)x; }are still reported and fixed.Note for reviewers: a lambda explicitly cast to a delegate type, such as
Select((Func<A, B>)(x => (B)x)), may no longer be reported. This errs on the safe side, and no existing test covered it.Tests
return.OptimizeLinqUsageAnalyzerTests(157 tests) pass on Roslyn 4.8, 4.14, 5.0, 5.6 and 5.9. The full suite was not run locally.Docs
docs/Rules/MA0078.mdnow states which selectors are reported, and its compliant example (Cast<object>(str), which did not compile) is fixed.