Fix SyntaxFactory.Identifier to throw ArgumentNullException instead of NullReferenceException#80699
Conversation
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
| Assert.Throws<ArgumentNullException>(() => SyntaxFactory.Identifier(null)); | ||
| } | ||
|
|
||
| [Fact] |
There was a problem hiding this comment.
@copilot These should have [WorkItem("...")] attributes identifying the github issue that prompted making these changes.
There was a problem hiding this comment.
Added WorkItem attributes to all test methods in commit d9a0c5e.
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
| Assert.Same(parseOptions, typeName.SyntaxTree.Options); | ||
| } | ||
|
|
||
| [Fact, WorkItem(17637, "https://github.com/dotnet/roslyn/issues/17637")] |
There was a problem hiding this comment.
@copilot you don't need the initial number argument with WorkItem(num, "..."). It's already in the url string. Please remove the first argument from all of these workitem attributes. #Closed
There was a problem hiding this comment.
Removed the redundant number argument from all WorkItem attributes in commit 1f8c4a2.
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
|
@dotnet/roslyn-compiler ptal. |
| { | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| SyntaxFactory.VerbatimIdentifier(SyntaxFactory.TriviaList(), "text", null, SyntaxFactory.TriviaList())); | ||
| } |
There was a problem hiding this comment.
@copilot Consolidate all the added tests into one single test #Closed
There was a problem hiding this comment.
Consolidated all 6 tests into a single test method in commit 8f7b5d3.
| public void Identifier_WithTrivia_NullText_ThrowsArgumentNullException() | ||
| { | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| Assert.Throws<ArgumentNullException>(() => |
There was a problem hiding this comment.
@CyrusNajmabadi Is this something we can automate or ask copilot to do with dotnet format?
Co-authored-by: jcouv <12466233+jcouv@users.noreply.github.com>
| { | ||
| Assert.Throws<ArgumentNullException>(() => SyntaxFactory.Identifier(null)); | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| SyntaxFactory.Identifier(SyntaxFactory.TriviaList(), null, SyntaxFactory.TriviaList())); |
There was a problem hiding this comment.
@copilot Add named arguments to the null and "value" arguments in this test to improve readability. Other arguments don't need to be named #Closed
There was a problem hiding this comment.
Added named arguments for text and valueText parameters in commit a4f2e8d.
Co-authored-by: jcouv <12466233+jcouv@users.noreply.github.com>
|
@dotnet/roslyn-compiler ptal. |
333fred
left a comment
There was a problem hiding this comment.
Signing off as I don't have a strong opinion on whether to polyfill or not, up to you @CyrusNajmabadi.
| /// <param name="text">The raw text of the identifier name, including any escapes or leading '@' character.</param> | ||
| public static SyntaxToken Identifier(string text) | ||
| { | ||
| if (text == null) |
There was a problem hiding this comment.
Nit: do we want to polyfill ArgumentNullException.ThrowIfNull?
There was a problem hiding this comment.
ok with not polyfilling.
Fix SyntaxFactory.Identifier(string) Null Reference Exception ✅
Issue
When calling
SyntaxFactory.XmlNameAttribute(null), the code threw aNullReferenceExceptiondeep in the internal syntax factory instead of the expectedArgumentNullException.Root Cause
The
SyntaxFactory.Identifiermethods did not validate that string parameters were not null before passing them to internal syntax factory methods.Changes Implemented
✅ Added null checks to all
SyntaxFactory.Identifieroverloads:Identifier(string text)Identifier(SyntaxTriviaList leading, string text, SyntaxTriviaList trailing)Identifier(SyntaxTriviaList leading, SyntaxKind contextualKind, string text, string valueText, SyntaxTriviaList trailing)VerbatimIdentifier(SyntaxTriviaList leading, string text, string valueText, SyntaxTriviaList trailing)✅ Added comprehensive test covering all overloads:
Identifier_Null_ThrowsArgumentNullException- consolidated test that validates all Identifier and VerbatimIdentifier overloads throwArgumentNullExceptionwhen passed null parameterstextandvalueTextparameters to improve readabilityTesting Results
✅ All 35 SyntaxFactoryTests pass
✅ All 9,735 syntax unit tests pass
✅ Manual verification confirms
XmlNameAttribute(null)now throwsArgumentNullExceptionwith parameter name "text"✅ Code review completed with no issues found
✅ All null checks follow the existing pattern used in the file
✅ Test includes WorkItem attribute referencing issue #17637
Impact
Fixes #17637
Original prompt
Fixes #17637
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.