C#: Add null-conditional (?.) support to CSharpPattern#6997
Merged
knutwannheden merged 1 commit intomainfrom Mar 17, 2026
Merged
C#: Add null-conditional (?.) support to CSharpPattern#6997knutwannheden merged 1 commit intomainfrom
?.) support to CSharpPattern#6997knutwannheden merged 1 commit intomainfrom
Conversation
The pattern comparator skipped all Markers during structural matching,
making `?.` and `.` indistinguishable. Check for the NullSafe marker
so patterns like `{obj}?.ToString()` only match null-conditional access.
?.) support to CSharpPattern
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
CSharpPatterncould not distinguish between regular member access (.) and null-conditional member access (?.). This meant a pattern like{obj}?.ToString()would incorrectly matchx.ToString(), and{obj}.ToString()would incorrectly matchx?.ToString(). Recipe authors targeting null-conditional expressions had no way to be precise about which access style they wanted to match.The root cause:
PatternMatchingComparatorskips allMarkersduring structural comparison, but?.is represented as aNullSafemarker on theFieldAccess.Name/MethodInvocation.Nameidentifier — so the two forms were structurally identical to the comparator.After this change, patterns correctly distinguish the two forms:
Changes
PatternMatchingComparator: CheckNullSafemarker presence when comparing nodes. If the pattern node has aNullSafemarker, the candidate must too (and vice versa).PatternMatchTests: 5 new tests covering null-conditional method calls, field access, exact matching, and negative cases for both directions (.pattern vs?.candidate and?.pattern vs.candidate).Test plan
PatternMatchTestspass (62 existing + 5 new)