chore: use ArgumentNullException.ThrowIfNull where applicable#1789
Merged
JamieMagee merged 1 commit intomainfrom Apr 27, 2026
Merged
chore: use ArgumentNullException.ThrowIfNull where applicable#1789JamieMagee merged 1 commit intomainfrom
JamieMagee merged 1 commit intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Updates PatternMatchingUtility.CompiledMatcher to use the .NET 6+ ArgumentNullException.ThrowIfNull pattern while preserving the existing constructor chaining behavior and exception semantics.
Changes:
- Replaces the inline
patterns ?? throw .../.ToArray()expression in theCompiledMatcher(IEnumerable<string>)constructor initializer with a helper method call. - Adds a private
ToArray(IEnumerable<string>)helper that performsThrowIfNulland converts tostring[]efficiently (avoiding enumeration when already an array).
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.ComponentDetection.Common/PatternMatchingUtility.cs | Introduces a ToArray helper to enable ArgumentNullException.ThrowIfNull in the CompiledMatcher constructor initializer while keeping behavior consistent. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 0
Replace pre-.NET 6 throw new ArgumentNullException(nameof(x)) patterns with ArgumentNullException.ThrowIfNull(x). Sites with custom exception messages are left unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
4fe9af0 to
e6bb4ac
Compare
|
👋 Hi! It looks like you modified some files in the
If none of the above scenarios apply, feel free to ignore this comment 🙂 |
grvillic
approved these changes
Apr 27, 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.
What
Replaces a pre-.NET 6
?? throw new ArgumentNullException(nameof(x))pattern withArgumentNullException.ThrowIfNull(x)(.NET 6+) inPatternMatchingUtility.CompiledMatcher.The inline
?? throwwas nested in a: this(...)constructor chain; it has been hoisted into a small privateToArrayhelper so the null check usesArgumentNullException.ThrowIfNullwhile preserving identical behavior (same exception type, sameParamName).Sites intentionally not changed
DotNetComponentandDependencyGraph.AddComponent("Invalid component node id") pass a descriptive second argument worth keeping.IsNullOrWhiteSpacechecks:ComponentRecorder.CreateSingleFileComponentRecorderandDependencyGraph.GetExplicitReferencedDependencyIdsthrowArgumentNullExceptionfor null and empty/whitespace strings, with explicit tests covering all three cases.ArgumentNullException.ThrowIfNullwould only handle the null case, so the existing pattern is preserved.Microsoft.ComponentDetection.Contractstargetsnetstandard2.0, whereArgumentNullException.ThrowIfNullis unavailable. The sites inNpmAuthorandFileComponentDetectorWithCleanupare left as-is.Why
ArgumentNullException.ThrowIfNullis the modern, intent-revealing form. It throws the same exception type with the sameParamName, so behavior is identical for callers and tests.Part 5 of a 6-PR cleanup series removing .NET-Framework-era anachronisms.