Repository navigation
feat: report a matcher that is compared as a value (Mockolate0007) - #1096
Conversation
[Medium impact] The PR appears safe to merge.
|
There was a problem hiding this comment.
All reported issues were addressed across 9 files
Reply with feedback, questions, or to request a fix.
Turn on auto-fix | Re-trigger cubic
Test Results 58 files ± 0 58 suites ±0 2h 11m 55s ⏱️ + 3m 45s Results for commit 5cb8414. ± Comparison against base commit dacdee7. This pull request removes 22 and adds 97 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
🚀 Benchmark ResultsCompared to Memory
Time
CompleteWorkflowBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 3.05GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 MockCreationBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 2.63GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CompleteUnconfiguredBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) INTEL XEON PLATINUM 8573C 3.60GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CompleteMethodBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 3.24GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CompletePropertyBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) Intel Xeon 6975P-C 3.80GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CompleteIndexerBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 2.63GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CompleteEventBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) Intel Xeon 6975P-C 2.89GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CallbackBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) Intel Xeon 6973P-C 4.10GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 AsyncBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 9V45 3.71GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 SequenceBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 |
Implements #1039. Every matcher converts to `object`, so a matcher passed where `Setup` or `Verify` takes an `object` value can bind as the value itself. The setup or verification then expects a call with the matcher object as argument and silently matches nothing: ```csharp public interface IFoo { void Take(object value); void Log(int level, params object[] args); } sut.Take(5); sut.Log(1, "a", 2); sut.Mock.Verify.Take(It.IsAny<int>()).Once(); // fails: It.IsAny<int>() is no matcher for object sut.Mock.Verify.Log(1, "a", It.IsAny<object>()).Once(); // fails: no overload mixes values with element matchers ``` #1030, #1031 and #1071 already fixed the cases where a matcher for the parameter type was compared as a value. What is left cannot work as written: a matcher for another type (`It.IsAny<int>()` for an `object` parameter, property or indexer value, or indexer key), element matchers of a `params object[]` or `params T[]` next to plain values, an `object` parameter above four parameters next to plain values in classic mode, and a type argument inferred from the matcher (`Setup.Accept(It.IsAny<int>())` on `Accept<T>(T value)` infers `T` as the matcher type). The new warning Mockolate0007 reports a matcher (a type implementing `IParameter`) that binds to a value parameter of a generated `Setup` or `Verify` member, or of `Set` on a property or indexer verification. It knows the cases that work at runtime and does not report them: a matcher for the value type on a parameter typed with a type parameter that can be `object` (the same rule the generator uses for #1071), and the matcher case of a union parameter. `It.Is<object>(matcher)` and an explicit `(object)` cast are not reported, so a matcher object can still be the expected value. The message says what to change, depending on why the matcher is compared as a value: - a matcher for another type: use a matcher for the value type; - a matcher for the value type next to plain values or matchers for other types: pass matchers of the parameter types for the other arguments as well; - a type argument inferred from the matcher: state the type arguments explicitly; - a custom matcher that is also a value of the parameter type: cast it to `IParameter<T>`, or, where even that converts to the value type, that no overload takes it as a matcher. Built against the generated code of `Mockolate.Tests` in classic (net10.0) and union (net11.0) mode, every reported call fails at runtime and every call that matches is not reported. The suite itself has one hit, `DynamicMembersTests` verifying `Dyn5("a", 2, 3, 4, It.IsAny<object>())`: in classic mode the matcher is compared as a value. The test only pins the failure message, so it keeps the call with the warning suppressed. Known gaps that the rule does not report: `It.IsOut`/`It.IsRef` matchers and `Match.AnyParameters()` in a value parameter, because they are no `IParameter`. For a lone `params T[]` of a generic method closed over `object` no spelling reaches the matcher overload; the rule reports it, but fixing it needs a generator change. The descriptor lives in `MatcherAsValueAnalyzer` instead of `Rules`, like Mockolate0003 and Mockolate0006, because release tracking reports RS2002 for an unshipped rule declared in another file. The conventions and the comment in `Rules.cs` now say so for all three.
The generator now infers the type argument of a generic method from a matcher, so a call like Setup.Plain(It.IsAny<int>()) binds the matcher overload and the hint to state the type arguments explicitly no longer applies.
… Set An explicit array passed to a params parameter binds the overload that takes values just like separate elements do, and Set on a property or indexer of a value type never treats a struct as a matcher at runtime, so both compared a matcher as a value without a warning.
bfec910 to
6784a65
Compare
… Mockolate0007 Match.AnyParameters() next to other arguments, as a params element, in Set or as an indexer key silently matches nothing, just like a matcher compared as a value. A type argument inferred from a matcher in a params array still happens next to plain values, so the hint pointed at a matcher for the matcher type. An explicit params array next to plain values needed two rounds of hints to fix.
A mocked type whose name contains SetupFor or VerifyFor made its raise and scenario interfaces look like setup or verify interfaces. The parameters match hint and the docs named Match.AnyParameters() for every parameters match and claimed a report where it binds the IParameters overload.
The interface prefix check runs for every argument in the compilation, so it must not allocate.
There was a problem hiding this comment.
All reported issues were addressed across 10 files
Reply with feedback, questions, or to request a fix.
Turn on auto-fix | Re-trigger cubic
…typed arrays A collection expression or a new[] array that converts to object[] passes the matcher as an element value just like an explicit object[], but the analyzer only looked into explicit array creations. The rule description now also names property and indexer verifications, which it reports as well.
|
… as a value (Mockolate0007) (#1096) by Valentin Breuß
… as a value (Mockolate0007) (#1096) by Valentin Breuß



Every matcher converts to
object, so a matcher passed whereSetuporVerifytakes anobjectvalue can bind as the value itself. The setup or verification then expects a call with the matcher object as argument and silently matches nothing:#1030, #1031 and #1071 already fixed the cases where a matcher for the parameter type was compared as a value. What is left cannot work as written: a matcher for another type (
It.IsAny<int>()for anobjectparameter, property or indexer value, or indexer key), element matchers of aparams object[]orparams T[]next to plain values, anobjectparameter above four parameters next to plain values in classic mode, and a type argument inferred from the matcher (Setup.Accept(It.IsAny<int>())onAccept<T>(T value)infersTas the matcher type).The new warning Mockolate0007 reports a matcher (a type implementing
IParameter) that binds to a value parameter of a generatedSetuporVerifymember, or ofSeton a property or indexer verification. It knows the cases that work at runtime and does not report them: a matcher for the value type on a parameter typed with a type parameter that can beobject(the same rule the generator uses for #1071), and the matcher case of a union parameter.It.Is<object>(matcher)and an explicit(object)cast are not reported, so a matcher object can still be the expected value.The message says what to change, depending on why the matcher is compared as a value:
IParameter<T>, or, where even that converts to the value type, that no overload takes it as a matcher.Built against the generated code of
Mockolate.Testsin classic (net10.0) and union (net11.0) mode, every reported call fails at runtime and every call that matches is not reported. The suite itself has one hit,DynamicMembersTestsverifyingDyn5("a", 2, 3, 4, It.IsAny<object>()): in classic mode the matcher is compared as a value. The test only pins the failure message, so it keeps the call with the warning suppressed.Known gaps that the rule does not report:
It.IsOut/It.IsRefmatchers andMatch.AnyParameters()in a value parameter, because they are noIParameter. For a loneparams T[]of a generic method closed overobjectno spelling reaches the matcher overload; the rule reports it, but fixing it needs a generator change.The descriptor lives in
MatcherAsValueAnalyzerinstead ofRules, like Mockolate0003 and Mockolate0006, because release tracking reports RS2002 for an unshipped rule declared in another file. The conventions and the comment inRules.csnow say so for all three.objectvalue slot is compared as a value #1039.