Skip to content

feat: report a matcher that is compared as a value (Mockolate0007) - #1096

Merged
vbreuss merged 7 commits into
mainfrom
feat/matcher-as-value-analyzer
Nov 21, 2026
Merged

vbreuss merged 7 commits into
mainfrom
feat/matcher-as-value-analyzer

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 21, 2026

Copy link
Copy Markdown
Member

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:

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.


@vbreuss vbreuss self-assigned this Nov 21, 2026
@vbreuss vbreuss added the enhancement New feature or request label Nov 21, 2026
@greptile-apps

greptile-apps Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Adds Mockolate0007 to warn when a matcher is treated as an expected value instead of matching calls.

  • Mockolate warns when a matcher is used as a value.

Reviews (3) · Last reviewed commit: "fix: report matchers in params collectio..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 9 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Test Results

    58 files  ±  0      58 suites  ±0   2h 11m 55s ⏱️ + 3m 45s
 5 441 tests + 81   5 441 ✅ + 81  0 💤 ±0  0 ❌ ±0 
53 707 runs  +243  53 707 ✅ +243  0 💤 ±0  0 ❌ ±0 

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.
 x-myHeader3: baz, True)
)
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
Mockolate.Tests.Web.ItExtensionsTests+IsHttpContentTests+WithHeadersTests ‑ ShouldVerifyStringHeaders(x-myHeader1 : foo
Mockolate.Tests.Web.ItExtensionsTests+IsHttpContentTests+WithHeadersTests ‑ ShouldVerifyStringHeaders(x-myHeader1 : foo
…
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenAMatcherForAnotherTypeBlocksTheOtherMatchers_ShouldSuggestMatchersForTheOtherArguments
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenAParametersMatchIsComparedAsAValue_ShouldBeFlagged(_ = verify[{|#0:Match·AnyParameters()|}];)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenAParametersMatchIsComparedAsAValue_ShouldBeFlagged(property·Set({|#0:Match·AnyParameters()|});)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenAParametersMatchIsComparedAsAValue_ShouldBeFlagged(verify·Log(1, {|#0:Match·AnyParameters()|});)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenAParametersMatchIsComparedAsAValue_ShouldBeFlagged(verify·Pair({|#0:Match·AnyParameters()|}, 1);)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenAParametersMatchWithAPredicateIsComparedAsAValue_ShouldBeFlagged
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenAnIndexerKeyIsMixedWithValues_ShouldSuggestMatchersForTheOtherArguments
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenMatcherBindsToADynamicValue_ShouldBeFlagged
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenMatcherBindsToTheValueCaseOfAUnion_ShouldBeFlagged
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenMatcherForAnotherTypeIsMixedWithValues_ShouldSuggestAMatcherForTheValueTypeFirst
…

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🚀 Benchmark Results

Compared to main, which ran on the same runner. Times that change by more than 10% are marked.

Memory

Benchmark main PR Δ
Indexer (N=1000) 105,576 B 105,577 B 🔴 +1 B (±0%)
Async (N=1000) 224,800 B 224,801 B 🔴 +1 B (±0%)

Time

Benchmark main PR Δ
Method (N=1) 147.0 ns 164.9 ns 🔴 +12%
Callback 188.9 ns 145.2 ns 🟢 -23%
Async (N=1) 245.3 ns 217.5 ns 🟢 -11%
CompleteWorkflowBenchmarks
Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 2.265 μs 0.0659 μs 0.0514 μs 1.00 5.14 KB 1.00
Imposter 2.338 μs 0.0204 μs 0.0181 μs 1.03 7.21 KB 1.40
TUnitMocks 5.563 μs 0.0214 μs 0.0201 μs 2.46 16.95 KB 3.30
Moq 1,416.258 μs 9.8763 μs 8.7551 μs 625.55 89.99 KB 17.51
NSubstitute 313.410 μs 0.5688 μs 0.4749 μs 138.43 53.11 KB 10.33
FakeItEasy 357.708 μs 4.1246 μs 3.8581 μs 158.00 63.94 KB 12.44
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
CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 15.27 ns 0.185 ns 0.144 ns 1.00 136 B 1.00
Imposter 86.90 ns 0.570 ns 0.505 ns 5.69 504 B 3.71
TUnitMocks 26.66 ns 0.214 ns 0.200 ns 1.75 216 B 1.59
Moq 1,286.38 ns 3.322 ns 2.945 ns 84.26 2088 B 15.35
NSubstitute 1,794.31 ns 9.912 ns 9.271 ns 117.54 5032 B 37.00
FakeItEasy 1,533.96 ns 8.127 ns 7.204 ns 100.48 2755 B 20.26
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
Unconfigured N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 433.4 ns 8.89 ns 8.32 ns 1.00 1.51 KB 1.00
Imposter 1 245.3 ns 8.99 ns 8.41 ns 0.57 1.06 KB 0.70
TUnitMocks 1 514.8 ns 15.23 ns 12.72 ns 1.19 1.81 KB 1.20
Moq 1 42,710.4 ns 490.75 ns 409.80 ns 98.57 9.88 KB 6.56
NSubstitute 1 6,160.3 ns 229.29 ns 214.48 ns 14.22 10.01 KB 6.64
FakeItEasy 1 5,044.7 ns 154.65 ns 137.09 ns 11.64 11.57 KB 7.67
Mockolate 10 1,551.0 ns 19.12 ns 17.88 ns 1.00 3.92 KB 1.00
Imposter 10 947.5 ns 11.28 ns 10.00 ns 0.61 2.89 KB 0.74
TUnitMocks 10 3,257.7 ns 67.87 ns 63.49 ns 2.10 8.47 KB 2.16
Moq 10 58,427.6 ns 833.94 ns 739.26 ns 37.68 24.63 KB 6.28
NSubstitute 10 18,057.1 ns 570.82 ns 533.95 ns 11.64 22.93 KB 5.85
FakeItEasy 10 20,995.2 ns 618.27 ns 548.08 ns 13.54 53.92 KB 13.75
Mockolate 1000 134,611.1 ns 3,155.46 ns 2,951.62 ns 1.00 266.89 KB 1.00
Imposter 1000 78,885.7 ns 1,845.28 ns 1,635.79 ns 0.59 203.99 KB 0.76
TUnitMocks 1000 309,949.9 ns 9,014.41 ns 8,432.08 ns 2.30 691.21 KB 2.59
Moq 1000 1,096,788.1 ns 22,041.06 ns 19,538.83 ns 8.15 1653.01 KB 6.19
NSubstitute 1000 1,195,891.9 ns 19,161.02 ns 16,985.75 ns 8.89 1434.38 KB 5.37
FakeItEasy 1000 2,126,025.5 ns 67,368.79 ns 63,016.81 ns 15.80 4690.62 KB 17.57
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
Method N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 164.9 ns 3.38 ns 3.16 ns 1.00 608 B 1.00
Imposter 1 171.2 ns 3.41 ns 3.19 ns 1.04 728 B 1.20
TUnitMocks 1 445.0 ns 16.54 ns 15.47 ns 2.70 2088 B 3.43
Moq 1 195,335.2 ns 740.38 ns 618.25 ns 1,184.83 14902 B 24.51
NSubstitute 1 6,215.5 ns 60.70 ns 56.78 ns 37.70 9320 B 15.33
FakeItEasy 1 5,828.1 ns 120.28 ns 112.51 ns 35.35 8160 B 13.42
Mockolate 10 402.3 ns 4.85 ns 4.54 ns 1.00 1112 B 1.00
Imposter 10 365.2 ns 6.57 ns 6.14 ns 0.91 1088 B 0.98
TUnitMocks 10 1,317.4 ns 20.02 ns 18.73 ns 3.28 3840 B 3.45
Moq 10 201,070.7 ns 1,147.97 ns 1,017.65 ns 499.88 18901 B 17.00
NSubstitute 10 9,245.4 ns 97.21 ns 90.93 ns 22.98 12345 B 11.10
FakeItEasy 10 9,480.6 ns 93.53 ns 87.49 ns 23.57 16393 B 14.74
Mockolate 1000 27,189.6 ns 462.09 ns 432.24 ns 1.00 56552 B 1.00
Imposter 1000 21,445.2 ns 272.26 ns 254.67 ns 0.79 40688 B 0.72
TUnitMocks 1000 98,141.8 ns 1,153.37 ns 1,078.87 ns 3.61 203585 B 3.60
Moq 1000 406,203.6 ns 5,111.75 ns 4,781.53 ns 14.94 431939 B 7.64
NSubstitute 1000 281,023.3 ns 2,997.51 ns 2,803.88 ns 10.34 378039 B 6.68
FakeItEasy 1000 378,342.0 ns 2,920.01 ns 2,731.38 ns 13.92 944362 B 16.70
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
Property N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 256.8 ns 1.13 ns 1.00 ns 1.00 920 B 1.00
Imposter 1 171.9 ns 1.42 ns 1.26 ns 0.67 672 B 0.73
TUnitMocks 1 424.2 ns 7.20 ns 6.74 ns 1.65 1672 B 1.82
Moq 1 7,087.1 ns 7.88 ns 7.37 ns 27.60 10680 B 11.61
NSubstitute 1 5,267.3 ns 15.81 ns 14.79 ns 20.52 11664 B 12.68
FakeItEasy 1 4,965.2 ns 16.43 ns 15.37 ns 19.34 11191 B 12.16
Mockolate 10 587.5 ns 2.98 ns 2.49 ns 1.00 1784 B 1.00
Imposter 10 338.3 ns 1.65 ns 1.54 ns 0.58 960 B 0.54
TUnitMocks 10 1,746.7 ns 4.94 ns 4.63 ns 2.97 4024 B 2.26
Moq 10 11,905.1 ns 23.50 ns 20.83 ns 20.26 18616 B 10.43
NSubstitute 10 11,591.1 ns 41.92 ns 37.16 ns 19.73 21528 B 12.07
FakeItEasy 10 12,900.2 ns 58.29 ns 45.51 ns 21.96 30780 B 17.25
Mockolate 1000 39,487.8 ns 204.51 ns 191.30 ns 1.00 96824 B 1.00
Imposter 1000 19,124.4 ns 129.44 ns 114.74 ns 0.48 32640 B 0.34
TUnitMocks 1000 124,187.3 ns 271.60 ns 240.76 ns 3.15 242489 B 2.50
Moq 1000 465,610.1 ns 1,312.21 ns 1,227.45 ns 11.79 836561 B 8.64
NSubstitute 1000 694,033.1 ns 2,206.66 ns 2,064.11 ns 17.58 1139620 B 11.77
FakeItEasy 1000 884,459.7 ns 13,559.50 ns 12,683.56 ns 22.40 2194285 B 22.66
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
Indexer N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 423.9 ns 7.64 ns 7.14 ns 1.00 1.64 KB 1.00
Imposter 1 300.7 ns 6.69 ns 6.25 ns 0.71 1.07 KB 0.65
TUnitMocks 1 709.9 ns 6.96 ns 6.17 ns 1.68 2.72 KB 1.66
Moq 1 225,739.2 ns 1,208.76 ns 1,071.53 ns 532.69 20.32 KB 12.38
NSubstitute 1 9,966.3 ns 137.51 ns 128.63 ns 23.52 12.73 KB 7.76
FakeItEasy 1 11,407.0 ns 281.96 ns 263.74 ns 26.92 13.27 KB 8.09
Mockolate 10 1,301.4 ns 9.12 ns 8.53 ns 1.00 2.55 KB 1.00
Imposter 10 774.4 ns 9.80 ns 8.19 ns 0.60 2.13 KB 0.83
TUnitMocks 10 2,675.2 ns 24.09 ns 21.35 ns 2.06 5.37 KB 2.10
Moq 10 239,211.6 ns 1,761.22 ns 1,647.44 ns 183.82 29.69 KB 11.62
NSubstitute 10 24,506.1 ns 96.11 ns 89.90 ns 18.83 25.52 KB 9.99
FakeItEasy 10 24,348.7 ns 141.10 ns 131.98 ns 18.71 32.47 KB 12.71
Mockolate 1000 95,904.8 ns 570.32 ns 505.58 ns 1.00 103.1 KB 1.00
Imposter 1000 49,353.0 ns 354.65 ns 331.74 ns 0.51 118.15 KB 1.15
TUnitMocks 1000 201,211.4 ns 1,401.65 ns 1,242.52 ns 2.10 293.67 KB 2.85
Moq 1000 1,049,034.2 ns 9,079.96 ns 7,582.18 ns 10.94 1006.46 KB 9.76
NSubstitute 1000 1,623,920.8 ns 12,615.62 ns 11,800.66 ns 16.93 1465.59 KB 14.21
FakeItEasy 1000 1,594,043.7 ns 27,890.13 ns 24,723.88 ns 16.62 2153 KB 20.88
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
Event Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 148.4 ns 1.23 ns 1.09 ns 1.00 712 B 1.00
Imposter 170.6 ns 0.91 ns 0.81 ns 1.15 512 B 0.72
TUnitMocks 192.2 ns 4.09 ns 3.83 ns 1.30 1392 B 1.96
Moq 9,911.4 ns 13.05 ns 10.19 ns 66.78 12761 B 17.92
NSubstitute 4,077.9 ns 15.58 ns 13.81 ns 27.47 9192 B 12.91
FakeItEasy 113,490.9 ns 568.48 ns 531.76 ns 764.61 15578 B 21.88
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
Callback Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 145.2 ns 2.68 ns 2.38 ns 1.00 800 B 1.00
Imposter 159.5 ns 8.05 ns 7.53 ns 1.10 616 B 0.77
TUnitMocks 403.3 ns 24.55 ns 22.96 ns 2.78 2056 B 2.57
Moq 34,924.2 ns 1,102.92 ns 977.71 ns 240.64 9046 B 11.31
NSubstitute 3,009.7 ns 175.42 ns 164.09 ns 20.74 7864 B 9.83
FakeItEasy 2,775.5 ns 154.25 ns 144.29 ns 19.12 6924 B 8.65
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
Async N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 217.5 ns 7.53 ns 7.05 ns 1.00 1 KB 1.00
Imposter 1 311.9 ns 8.81 ns 8.24 ns 1.44 1.58 KB 1.58
TUnitMocks 1 784.8 ns 16.22 ns 15.17 ns 3.61 4.36 KB 4.36
Moq 1 113,667.6 ns 1,682.58 ns 1,573.89 ns 523.00 22.77 KB 22.77
NSubstitute 1 8,551.6 ns 145.50 ns 128.98 ns 39.35 19.09 KB 19.09
FakeItEasy 1 6,643.4 ns 231.92 ns 216.94 ns 30.57 14 KB 14.00
Mockolate 10 1,070.2 ns 25.63 ns 23.97 ns 1.00 2.97 KB 1.00
Imposter 10 1,062.1 ns 26.83 ns 25.09 ns 0.99 3.41 KB 1.15
TUnitMocks 10 3,503.0 ns 39.60 ns 30.91 ns 3.27 10.29 KB 3.47
Moq 10 129,251.7 ns 2,133.68 ns 1,995.85 ns 120.83 40.19 KB 13.54
NSubstitute 10 15,987.7 ns 356.96 ns 333.90 ns 14.95 29.4 KB 9.90
FakeItEasy 10 27,286.9 ns 872.73 ns 816.36 ns 25.51 59.08 KB 19.90
Mockolate 1000 97,941.9 ns 1,929.48 ns 1,804.84 ns 1.00 219.53 KB 1.00
Imposter 1000 85,379.4 ns 431.24 ns 403.38 ns 0.87 204.5 KB 0.93
TUnitMocks 1000 342,606.0 ns 2,824.35 ns 2,641.89 ns 3.50 687.49 KB 3.13
Moq 1000 2,523,303.9 ns 42,449.03 ns 39,706.85 ns 25.77 1891.03 KB 8.61
NSubstitute 1000 775,066.1 ns 5,719.90 ns 5,350.40 ns 7.92 1092.89 KB 4.98
FakeItEasy 1000 2,967,862.1 ns 147,831.87 ns 138,282.02 ns 30.31 4933.41 KB 22.47
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
Sequence Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 2.606 μs 0.0389 μs 0.0364 μs 1.00 2.23 KB 1.00
Imposter 2.383 μs 0.0266 μs 0.0249 μs 0.91 2.23 KB 1.00
TUnitMocks 2.929 μs 0.0145 μs 0.0121 μs 1.12 3.27 KB 1.46
Moq 9.976 μs 0.1405 μs 0.1314 μs 3.83 10.79 KB 4.83
NSubstitute 8.784 μs 0.1764 μs 0.1650 μs 3.37 14.29 KB 6.40
FakeItEasy 9.072 μs 0.0628 μs 0.0587 μs 3.48 14.28 KB 6.39
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  

@vbreuss
vbreuss marked this pull request as draft November 21, 2026 07:12
vbreuss and others added 3 commits November 21, 2026 12:14
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.
@vbreuss
vbreuss force-pushed the feat/matcher-as-value-analyzer branch from bfec910 to 6784a65 Compare November 21, 2026 11:22
… 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.
@vbreuss
vbreuss marked this pull request as ready for review November 21, 2026 11:57

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.
@vbreuss
vbreuss enabled auto-merge (squash) November 21, 2026 12:33
@sonarqubecloud

Copy link
Copy Markdown

@vbreuss
vbreuss merged commit 3094c0e into main Nov 21, 2026
28 checks passed
@vbreuss
vbreuss deleted the feat/matcher-as-value-analyzer branch November 21, 2026 12:48
github-actions Bot added a commit that referenced this pull request Nov 21, 2026
… as a value (Mockolate0007) (#1096) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Nov 21, 2026
… as a value (Mockolate0007) (#1096) by Valentin Breuß
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analyzer: a matcher bound to an object value slot is compared as a value

1 participant