mock: do not let a surplus matcher match a missing argument - #1938
Merged
brackendawson merged 2 commits intoAug 24, 2026
Conversation
Arguments.Diff used the plain string "(Missing)" as the sentinel for an
argument that one side does not have. A surplus mock.Anything,
mock.AnythingOfType("string"), mock.IsType(""), or the literal string
"(Missing)" therefore matched an argument the caller never passed, so an
expectation with more arguments than the method takes passed silently.
mock.MatchedBy was handed the sentinel string as if it were a real
argument, and mock.FunctionalOptions() panicked on it.
The sentinel is now an unexported missingArgument type with no
constructor, so no expectation can hold a value equal to it. Diff also
reports a difference for a missing argument on either side before any
matcher is consulted, since differing arity is never a match.
Failure messages are unchanged: missing arguments still print as
"(Missing)".
Collaborator
|
Can you write a PR summary using our template please. |
brackendawson
approved these changes
Aug 24, 2026
brackendawson
left a comment
Collaborator
There was a problem hiding this comment.
Lovely, thank you. 👍
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
A matcher in a surplus position could match an argument that was never passed.
Changes
Arguments.Diffused the string"(Missing)"to stand in for an absent argument. That is now an unexportedmissingArgumenttype.Diffrecords the length mismatch before it consults any matcher.MatchedByis no longer called with the sentinel.missingArgument.String()returns(Missing).Motivation
With a plain string as the sentinel,
mock.Anything,mock.AnythingOfType("string"),mock.IsType("")and the literal"(Missing)"all matched an argument that did not exist, so anOncall listing too many arguments passed instead of failing.mock.FunctionalOptions()panicked on it.Related issues
Closes #1211
Closes #587