Fix crash in LinqEnumerableCodeFixProvider with conditional access expressions (#216)#240
Open
Copilot wants to merge 3 commits into
Open
Fix crash in LinqEnumerableCodeFixProvider with conditional access expressions (#216)#240Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
…pressions When the code fix wraps an expression containing a MemberBindingExpression (from conditional access ?.) in parentheses with Simplifier.Annotation, Roslyn's speculative semantic model crashes with NullReferenceException in FindConditionalAccessNodeForBinding. The fix skips parenthesization when the expression contains a MemberBindingExpression, which avoids triggering the Roslyn bug. Fixes #216 Agent-Logs-Url: https://github.com/WiseTechGlobal/WTG.Analyzers/sessions/d974a4ba-43c4-4b13-acca-e9ccb83f1f8f Co-authored-by: brian-reichle <18721383+brian-reichle@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
brian-reichle
May 26, 2026 05:19
View session
Replace ContainsMemberBinding descendant traversal with a simple IsKind(SyntaxKind.InvocationExpression) check, since invocation expressions never need parenthesization regardless of their receiver. Agent-Logs-Url: https://github.com/WiseTechGlobal/WTG.Analyzers/sessions/fc763f58-f56a-4811-a94b-2fe440b3eb00 Co-authored-by: brian-reichle <18721383+brian-reichle@users.noreply.github.com>
Agent-Logs-Url: https://github.com/WiseTechGlobal/WTG.Analyzers/sessions/efaaa8ee-40eb-44f9-a086-cd09f2840505 Co-authored-by: brian-reichle <18721383+brian-reichle@users.noreply.github.com>
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.
Summary
Fixes #216 - VS crash when applying WTG3013 code fix on expressions using conditional access (
?.).Root Cause Analysis
The crash is a bug in Roslyn (specifically in
SyntaxFactory.FindConditionalAccessNodeForBinding), but triggered by the analyzer's code fix producing a tree that Roslyn's simplifier cannot handle.When
LinqEnumerableCodeFixProviderfixes a.Concat()call that's part of a conditional access chain like:The code fix wraps the receiver expression (
.Where(x => x > 0)) inParenthesizedExpressionwithSimplifier.Annotation. This disconnects theMemberBindingExpression(.Where) from its parentConditionalAccessExpression. When Roslyn's simplifier then tries to evaluate whether the parentheses can be removed via speculative semantic analysis, it crashes with aNullReferenceException.Fix
Added a
WrapExpressionIfNeededhelper that skips parenthesization when the expression is anInvocationExpression. Invocation expressions never need parenthesization regardless of their receiver, as they already have clear binding. This avoids the issue with conditional access chains while also being a simpler and faster check.Changes
WrapExpressionIfNeededhelper that skips wrapping forInvocationExpressionnodesFixConcatWithAppendMethodandFixConcatWithPrependMethodConditionalAccesstest case that reproduces the crash