Fix Anaylzers treating passed in variable argument name as null#640
Merged
YunchuWang merged 4 commits intomainfrom Feb 17, 2026
Merged
Fix Anaylzers treating passed in variable argument name as null#640YunchuWang merged 4 commits intomainfrom
YunchuWang merged 4 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug where all orchestration analyzers (DateTime, Delay, Guid, etc.) throw ArgumentNullException when AddOrchestratorFunc is called with a non-literal name argument (e.g., a variable from a foreach loop). The fix adds a simple null check in RoslynExtensions.GetConstantValueFromAttribute to return default when the argument is not a string literal, allowing the caller to gracefully handle the case.
Changes:
- Added null guard in
GetConstantValueFromAttributeto prevent crashes when name argument is not a literal - Added 5 new unit tests across DateTime, Delay, and Guid analyzer tests to cover the foreach + variable name scenario
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Analyzers/RoslynExtensions.cs | Added null check to prevent ArgumentNullException when argument is not a string literal |
| test/Analyzers.Tests/Orchestration/DateTimeOrchestrationAnalyzerTests.cs | Added NoDiag and HasDiag tests for foreach with variable name scenario |
| test/Analyzers.Tests/Orchestration/DelayOrchestrationAnalyzerTests.cs | Added NoDiag and HasDiag tests for foreach with variable name scenario |
| test/Analyzers.Tests/Orchestration/GuidOrchestrationAnalyzerTests.cs | Added NoDiag test for foreach with variable name scenario |
…sts.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
test/Analyzers.Tests/Orchestration/GuidOrchestrationAnalyzerTests.cs
Outdated
Show resolved
Hide resolved
…sts.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
kaibocai
approved these changes
Feb 17, 2026
This was referenced Mar 18, 2026
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
What changed?
RoslynExtensions.GetConstantValueFromAttributeto handle cases where thenameargument toAddOrchestratorFuncis not a string literal (e.g., a loop variable in aforeach).DateTimeOrchestrationAnalyzerTests,DelayOrchestrationAnalyzerTests, andGuidOrchestrationAnalyzerTeststo cover the foreach + variable name scenario.Why is this change needed?
All orchestration analyzers (
DateTimeOrchestrationAnalyzer,DelayOrchestrationAnalyzer,GuidOrchestrationAnalyzer, etc.) throwSystem.ArgumentNullExceptionwith messageValue cannot be null. (Parameter 'node')whenAddOrchestratorFuncis called with an inline lambda inside aforeachloop that captures a loop-scoped variable. This surfaces asAD0001errors and breaks builds withTreatWarningsAsErrors.The root cause is that
GetConstantValueFromAttributesearches for aLiteralExpressionSyntaxdescendant in the argument's syntax. When the name is a variable rather than a string literal,FirstOrDefault()returnsnull, which is passed directly toSemanticModel.GetConstantValue(), causing the crash.Issues / work items
Project checklist
release_notes.mdAI-assisted code disclosure (required)
Was an AI tool used? (select one)
If AI was used:
src/Analyzers/RoslynExtensions.cs— null guard inGetConstantValueFromAttributetest/Analyzers.Tests/Orchestration/DateTimeOrchestrationAnalyzerTests.cs— 2 new teststest/Analyzers.Tests/Orchestration/DelayOrchestrationAnalyzerTests.cs— 2 new teststest/Analyzers.Tests/Orchestration/GuidOrchestrationAnalyzerTests.cs— 1 new testAI verification (required if AI was used):
Testing
Automated tests
New tests added:
FuncOrchestratorInForEachWithVariableNameNoDiagDateTimeOrchestrationAnalyzerTests.csFuncOrchestratorInForEachWithVariableNameHasDiagDateTimeOrchestrationAnalyzerTests.csDateTime.Nowinside foreach lambdaFuncOrchestratorInForEachWithVariableNameNoDiagDelayOrchestrationAnalyzerTests.csFuncOrchestratorInForEachWithVariableNameHasDiagDelayOrchestrationAnalyzerTests.csThread.Sleepinside foreach lambdaFuncOrchestratorInForEachWithVariableNameNoDiagGuidOrchestrationAnalyzerTests.csManual validation (only if runtime/behavior changed)
Notes for reviewers
RoslynExtensions.GetConstantValueFromAttribute. When the name argument is not a string literal, the method now returnsdefaultinstead of crashing. The caller inOrchestrationAnalyzer.cs(line 169) already handles this gracefully vianame.Value?.ToString() ?? methodSymbol.Name.