Skip to content

Fix MA0182 false positive for delegates used in new expressions#969

Merged
meziantou merged 2 commits intomainfrom
copilot/fix-ma0182-false-positive-again
Jan 14, 2026
Merged

Fix MA0182 false positive for delegates used in new expressions#969
meziantou merged 2 commits intomainfrom
copilot/fix-ma0182-false-positive-again

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 14, 2026

MA0182 incorrectly reports internal delegate types as unused when instantiated via new DelegateType(...), commonly seen with Moq's .Callback() method.

Changes

  • Register handler for OperationKind.DelegateCreation to track delegate instantiation
  • Implement AnalyzeDelegateCreation to mark delegate types as used

Example

Previously flagged incorrectly:

// MA0182: Internal type 'tryAcquireLockDelegate' is apparently never used
private delegate void tryAcquireLockDelegate(string key, out string lockToken);

mockClient
    .Setup(x => x.TryAcquireLock(..., out outParam))
    .Callback(new tryAcquireLockDelegate((string key, out string lockToken) => 
    {
        lockToken = "42";
    }));

Now correctly recognized as used since new tryAcquireLockDelegate(...) creates an IDelegateCreationOperation.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.nuget.org
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/Meziantou.Analyzer.Test.runtimeconfig.json --depsfile /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/Meziantou.Analyzer.Test.deps.json /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/testhost.dll --port 46135 --endpoint 127.0.0.1:046135 --role client --parentprocessid 4631 --datacollectionport 41329 --telemetryoptedin false (dns block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/Meziantou.Analyzer.Test.runtimeconfig.json --depsfile /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/Meziantou.Analyzer.Test.deps.json /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/testhost.dll --port 44919 --endpoint 127.0.0.1:044919 --role client --parentprocessid 4832 --datacollectionport 39795 --telemetryoptedin false (dns block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/Meziantou.Analyzer.Test.runtimeconfig.json --depsfile /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/Meziantou.Analyzer.Test.deps.json /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Release/net10.0/testhost.dll --port 45733 --endpoint 127.0.0.1:045733 --role client --parentprocessid 5438 --datacollectionport 40723 --telemetryoptedin false ziantou.Analyzer--ignore-submodules (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>MA0182 false positive using delegate</issue_title>
<issue_description>### Version of the Meziantou.Analyzer NuGet package

2.0.277

Rule Identifier

MA0182

Target Framework

Net 10.0

C# Language version

C# 12

Description

Reported error

Error MA0182 : Internal type 'tryAcquireLockDelegate' is apparently never used. If so, remove it from the assembly. If this type is intended to contain only static members, make it 'static'. (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0182.md)

but tryAcquireLockDelegate in use in Mock .Callback

Reproduction Steps

Minimal code:

[TestFixture]
[TestOf(typeof(Program))]
public class ProgramTest
{
	public interface IInterface
	{
		bool TryAcquireLock(string key, string comment, TimeSpan timeout, out string lockToken);
	}

	private delegate void tryAcquireLockDelegate(string key, string comment, TimeSpan timeout, out string lockToken);

	[Test]
	public void Test()
	{
		var mockLockManagerClient = new Mock<IInterface>();

		string outParam;
		mockLockManagerClient
			.Setup(x => x.TryAcquireLock(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<TimeSpan>(), out outParam))
			.Returns(true)
			.Callback(new tryAcquireLockDelegate((string key, string comment, TimeSpan timeout, out string lockToken) =>
			{
				lockToken = "42";
			}));
	}
}

Other information

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix MA0182 false positive using delegate Fix MA0182 false positive for delegates used in new expressions Jan 14, 2026
Copilot AI requested a review from meziantou January 14, 2026 21:54
@meziantou meziantou marked this pull request as ready for review January 14, 2026 21:56
@meziantou meziantou enabled auto-merge (squash) January 14, 2026 21:56
@meziantou meziantou merged commit 8e59325 into main Jan 14, 2026
13 checks passed
@meziantou meziantou deleted the copilot/fix-ma0182-false-positive-again branch January 14, 2026 22:01
This was referenced Jan 14, 2026
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.

MA0182 false positive using delegate

2 participants