Fix: Support compound assignments in "Use null propagation" analyzer#81328
Merged
CyrusNajmabadi merged 6 commits intomainfrom Nov 19, 2025
Merged
Conversation
Modified Unwrap method to detect and handle compound assignments (+=, -=, etc.) by checking if the first child node is on the left side of a compound assignment. Added three tests for compound assignment scenarios with events and numeric values. Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Compound assignments with null conditional (?.) are only supported in C# 14+. Added language version check to ensure the diagnostic is only offered when supported. Added test to verify diagnostic is not offered for C# 13. Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix null propagation for compound assignments
Fix: Support compound assignments in "Use null propagation" analyzer
Nov 19, 2025
src/Analyzers/CSharp/Tests/UseNullPropagation/UseNullPropagationTests.cs
Outdated
Show resolved
Hide resolved
| // Check if node itself is a compound assignment. We do this by checking if the first child node | ||
| // is on the left side of a compound assignment (which means the node is the compound assignment itself). | ||
| // Compound assignments with null conditional (?.) are only supported in C# 14+. | ||
| var firstChild = node.ChildNodesAndTokens().FirstOrDefault().AsNode(); |
Contributor
There was a problem hiding this comment.
@copilot merge with above if statement. It should be possible to use a syntaxfacts helper like IsAnyAssignment
Contributor
Author
There was a problem hiding this comment.
Merged the checks using IsLeftSideOfAnyAssignment in commit bc99892. This consolidated both simple and compound assignment checks into a single if statement.
- Added WorkItem attribute to TestIfStatement_CompoundAssignment_Event test - Merged simple and compound assignment checks using IsLeftSideOfAnyAssignment helper - Simplified code by consolidating the two separate if statements into one Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
JoeRobich
approved these changes
Nov 19, 2025
jnm2
reviewed
Nov 24, 2025
| // | ||
| // If 'expr' is not null, then we execute the body and then end up with expr being null. So `expr?.Method(); expr = null;` | ||
| // preserves those semantics. Simialrly, if is expr is null, then `expr?.Method();` does nothing, and `expr = null` keeps it | ||
| // preserves those semantics. Simialarly, if is expr is null, then `expr?.Method();` does nothing, and `expr = null` keeps it |
Contributor
There was a problem hiding this comment.
I love how copilot found this typo and... made it worse 😂
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: Add support for compound assignments in "Use null propagation" analyzer
This PR fixes the issue where the "Use null propagation" code fix was not being offered for compound assignments (like
+=,-=,*=, etc.) in if statements.Problem
The analyzer would offer the diagnostic for simple assignments:
But not for compound assignments:
Solution
Modified the
Unwrapmethod inAbstractUseNullPropagationDiagnosticAnalyzer.csto detect compound assignments usingIsLeftSideOfAnyAssignmenthelper which handles both simple and compound assignments in a unified way.Changes Made
Code Changes:
src/Analyzers/Core/Analyzers/UseNullPropagation/AbstractUseNullPropagationDiagnosticAnalyzer.csIsLeftSideOfAnyAssignmenthelperTest Changes:
src/Analyzers/CSharp/Tests/UseNullPropagation/UseNullPropagationTests.csWorkItemattribute linking to issue "Use null propagation" doesn't show up for compound assignments #81322TestIfStatement_CompoundAssignment_Event- Tests event subscription with+=TestIfStatement_CompoundAssignment_AddAssignment- Tests numeric addition with+=TestIfStatement_CompoundAssignment_SubtractAssignment- Tests numeric subtraction with-=TestIfStatement_CompoundAssignment_NotAvailableInCSharp13- Tests language version checkTest Results
✅ All 126 tests passing (125 existing + 4 new tests)
✅ No security issues detected by CodeQL
Example Usage
Before:
After (with C# 14+):
Fixes #81322
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.