Skip to content

chore: use ArgumentNullException.ThrowIfNull where applicable#1789

Merged
JamieMagee merged 1 commit intomainfrom
cleanup/argnull-throwifnull
Apr 27, 2026
Merged

chore: use ArgumentNullException.ThrowIfNull where applicable#1789
JamieMagee merged 1 commit intomainfrom
cleanup/argnull-throwifnull

Conversation

@JamieMagee
Copy link
Copy Markdown
Member

What

Replaces a pre-.NET 6 ?? throw new ArgumentNullException(nameof(x)) pattern with ArgumentNullException.ThrowIfNull(x) (.NET 6+) in PatternMatchingUtility.CompiledMatcher.

The inline ?? throw was nested in a : this(...) constructor chain; it has been hoisted into a small private ToArray helper so the null check uses ArgumentNullException.ThrowIfNull while preserving identical behavior (same exception type, same ParamName).

Sites intentionally not changed

  • Custom messages: DotNetComponent and DependencyGraph.AddComponent ("Invalid component node id") pass a descriptive second argument worth keeping.
  • IsNullOrWhiteSpace checks: ComponentRecorder.CreateSingleFileComponentRecorder and DependencyGraph.GetExplicitReferencedDependencyIds throw ArgumentNullException for null and empty/whitespace strings, with explicit tests covering all three cases. ArgumentNullException.ThrowIfNull would only handle the null case, so the existing pattern is preserved.
  • netstandard2.0 projects: Microsoft.ComponentDetection.Contracts targets netstandard2.0, where ArgumentNullException.ThrowIfNull is unavailable. The sites in NpmAuthor and FileComponentDetectorWithCleanup are left as-is.

Why

ArgumentNullException.ThrowIfNull is the modern, intent-revealing form. It throws the same exception type with the same ParamName, so behavior is identical for callers and tests.

Part 5 of a 6-PR cleanup series removing .NET-Framework-era anachronisms.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 the CompiledMatcher(IEnumerable<string>) constructor initializer with a helper method call.
  • Adds a private ToArray(IEnumerable<string>) helper that performs ThrowIfNull and converts to string[] 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>
@JamieMagee JamieMagee force-pushed the cleanup/argnull-throwifnull branch from 4fe9af0 to e6bb4ac Compare April 27, 2026 23:17
@github-actions
Copy link
Copy Markdown

👋 Hi! It looks like you modified some files in the Detectors folder.
You may need to bump the detector versions if any of the following scenarios apply:

  • The detector detects more or fewer components than before
  • The detector generates different parent/child graph relationships than before
  • The detector generates different devDependencies values than before

If none of the above scenarios apply, feel free to ignore this comment 🙂

@JamieMagee JamieMagee merged commit 5dd061e into main Apr 27, 2026
15 checks passed
@JamieMagee JamieMagee deleted the cleanup/argnull-throwifnull branch April 27, 2026 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants