Skip to content

mock: do not let a surplus matcher match a missing argument - #1938

Merged
brackendawson merged 2 commits into
stretchr:masterfrom
dylanpulver:fix/mock-missing-arg-sentinel
Aug 24, 2026
Merged

mock: do not let a surplus matcher match a missing argument#1938
brackendawson merged 2 commits into
stretchr:masterfrom
dylanpulver:fix/mock-missing-arg-sentinel

Conversation

@dylanpulver

@dylanpulver dylanpulver commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Summary

A matcher in a surplus position could match an argument that was never passed.

Changes

  • Arguments.Diff used the string "(Missing)" to stand in for an absent argument. That is now an unexported missingArgument type.
  • Diff records the length mismatch before it consults any matcher.
  • MatchedBy is no longer called with the sentinel.
  • Printed output is unchanged. 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 an On call listing too many arguments passed instead of failing. mock.FunctionalOptions() panicked on it.

Related issues

Closes #1211
Closes #587

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)".
@brackendawson

brackendawson commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Can you write a PR summary using our template please.

@brackendawson brackendawson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Lovely, thank you. 👍

@brackendawson
brackendawson merged commit 9f9d4f4 into stretchr:master Aug 24, 2026
11 checks passed
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.

mock: nothing does match mock.Anything mock does not enforce arity if parameter is Anything

2 participants