Throw ArgumentNullException on StringFnV1AHasher.Hash(null) - #72
Merged
Conversation
Public APIs should signal a null argument as a contract violation, not as an unchecked dereference. Adds ArgumentNullException.ThrowIfNull at the top of Hash, declares the exception in the XML doc, and updates the test that previously pinned the NullReferenceException wart. In practice the dictionaries route the out-of-band default-key entry around the hasher, so this only surfaces for direct hasher use or for custom IHashProvider<string> consumers that don't handle null keys. Closes #71.
Owner
Author
|
@copilot resolve the merge conflicts in this pull request |
Co-authored-by: marius-bughiu <11870708+marius-bughiu@users.noreply.github.com>
Contributor
Merged |
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
Closes #71. Replaces the
NullReferenceExceptionthatStringFnV1AHasher.Hash(null)currently throws with an explicitArgumentNullException(parameter name"key"), and updates the test that previously pinned the NRE behaviour.NullReferenceExceptionfrom a public API is a code-quality wart per .NET framework design guidelines — it conflates a programmer error (unchecked dereference) with what is really a contract violation. The previous test even openly noted the wart: "If you'd prefer an explicit ArgumentNullException, modify the struct." This PR makes that change.Surface area
The Celerity dictionaries route the out-of-band
null/default(TKey)key entry around the hasher and never callHash(null), so existing dictionary callers see no behaviour change. The newArgumentNullExceptiononly surfaces when:StringFnV1AHasherdirectly (e.g. as a building block in their own collection or a hash-quality test harness), orStringFnV1AHasherinto a customIHashProvider<string>consumer that does not handle the null-key slot itself.In both cases the new exception is the correct one — and the regression test pins it down.
Files
src/Celerity/Hashing/StringFnV1AHasher.cs—ArgumentNullException.ThrowIfNull(key)at the top ofHash, plus a full XML doc on the method declaring the exception.src/Celerity.Tests/Hashing/StringFnV1AHasherTests.cs—Hash_NullString_ThrowsNullReferenceExceptionis renamed toHash_NullString_ThrowsArgumentNullExceptionand asserts both the exception type andParamName == "key".CHANGELOG.md—[Unreleased]Fixedentry.Test plan
dotnet buildclean (0 errors; 55 pre-existing xUnit2013 warnings in unrelated test files)StringFnV1AHasherTestson Linux / Windows / macOS — local runtime is .NET 10 only and the projects target net8.0, so the test execution gate is CIStringFnV1AHasherTests.Hash_NullString_ThrowsArgumentNullException:Hash(null!)throwsArgumentNullExceptionParamNameis"key"