Fix trimming analyzer missing diagnostics for method calls in field access expressions #123040
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.
Description
The trimming analyzer fails to report diagnostics when method calls are used in field access expressions. For example,
type.GetInterfaceMap(@interface).TargetMethodsdoes not warn, whiletype.GetInterfaceMap(@interface)correctly reports IL2072.Root cause:
TrimAnalysisVisitor.VisitFieldReferencedoes not visit the field's instance operation before processing the field itself.Fix: Visit the instance before processing the field, consistent with how other visitor methods handle instances (property references, event references).
Customer Impact
Silent trimming failures. Code that should warn about missing annotations passes analysis when the return value is immediately used for field access, leading to runtime failures in trimmed applications.
Regression
No. This is a long-standing analyzer gap, not a recent regression.
Testing
GetInterfaceDataFlow.cscovering direct calls, field access, and nested field accessRisk
Low. The change is minimal (4 lines) and aligns with established patterns in the codebase. It only affects diagnostic reporting—no changes to trimming behavior or false positives. The fix makes the analyzer more correct by eliminating false negatives.
Original prompt
Type.GetInterfaceMap(Type)is treated oddly by the trimming analyzers #108114💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.