Skip to content

Fix NRE in WTG1010 when analyzing conditional invoke expressions#238

Open
Copilot wants to merge 2 commits into
masterfrom
copilot/fix-nre-in-wtg1010-analyzer
Open

Fix NRE in WTG1010 when analyzing conditional invoke expressions#238
Copilot wants to merge 2 commits into
masterfrom
copilot/fix-nre-in-wtg1010-analyzer

Conversation

Copilot AI commented May 26, 2026

Copy link
Copy Markdown

VarAnalyzer.AnalyzeInvoke throws a NullReferenceException when processing conditional access invocations (e.g., obj?.TryGetValue("key", out object val)). The speculative binding of the rewritten InvocationExpressionSyntax fails because a MemberBindingExpression node lacks the conditional access receiver context that Roslyn's binder requires.

Changes

  • VarAnalyzer.cs — When the invocation expression is a MemberBindingExpressionSyntax inside a ConditionalAccessExpressionSyntax, rewrite the speculative expression as a regular MemberAccessExpression using the conditional access target so the binder can resolve the receiver.
  • VarFixAllProvider.cs — Same fix applied to the ReplaceTypes method which has the identical speculative binding pattern.
  • Test data — Added conditional access test case (Lookup?.TryGetValue("key", out string value)) to the Out sample.
if (invoke.Expression is MemberBindingExpressionSyntax memberBinding
    && invoke.Parent is ConditionalAccessExpressionSyntax conditionalAccess)
{
    var memberAccess = SyntaxFactory.MemberAccessExpression(
        SyntaxKind.SimpleMemberAccessExpression,
        conditionalAccess.Expression,
        memberBinding.Name);
    speculativeExpression = proposedInvoke.WithExpression(memberAccess);
    speculativePosition = conditionalAccess.SpanStart;
}

When the invocation's expression is a MemberBindingExpression (e.g.,
obj?.TryGetValue(...)), rewrite the speculative expression as a regular
member access to provide the necessary binding context for Roslyn's
speculative symbol resolution.

Fixes the NullReferenceException that occurred because the invocation
node alone doesn't contain the conditional access target information
needed by the binder.

Agent-Logs-Url: https://github.com/WiseTechGlobal/WTG.Analyzers/sessions/62f30448-c8d6-4608-8142-a1c3fece533b

Co-authored-by: brian-reichle <18721383+brian-reichle@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NRE in WTG1010 analyzer for conditional invoke Fix NRE in WTG1010 when analyzing conditional invoke expressions May 26, 2026
Copilot AI requested a review from brian-reichle May 26, 2026 04:03
@brian-reichle brian-reichle marked this pull request as ready for review May 26, 2026 04:34
@brian-reichle brian-reichle requested a review from yaakov-h as a code owner May 26, 2026 04:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NRE in WTG1010 when analyzing a conditional invoke.

2 participants