Skip to content

Fix MA0042 false positive when non-generic method has a same-named generic overload with awaitable return type#1102

Merged
meziantou merged 2 commits intomainfrom
copilot/fix-false-positive-ma0042
Apr 24, 2026
Merged

Fix MA0042 false positive when non-generic method has a same-named generic overload with awaitable return type#1102
meziantou merged 2 commits intomainfrom
copilot/fix-false-positive-ma0042

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 24, 2026

MA0042 incorrectly reports a diagnostic when a non-generic method (e.g., TUnit's Assert.That(string?)) coexists with a same-named generic overload (e.g., Assert.That<T>(T)) that returns an awaitable type. The suggested fix (await Assert.That(x)) fails to compile because C# overload resolution always prefers non-generic methods — the call still resolves to the non-awaitable non-generic overload.

Changes

  • DoNotUseBlockingCallInAsyncContextAnalyzer.cs: In FindPotentialAsyncEquivalent, skip generic candidates when performing a same-name search (methodName == targetMethod.Name) and the original method is non-generic. Since the compiler will always prefer the non-generic overload at the call site, the generic awaitable variant can never actually be selected by applying await.

  • DoNotUseBlockingCallInAsyncContextAnalyzer_AsyncContextTests.cs: Regression test covering the pattern below.

static class Assert
{
    public static ValueAssertion  That(string? value) => ...;   // non-awaitable
    public static ValueAssertion<T> That<T>(T value) => ...;    // awaitable
}

// MA0042 was incorrectly flagged here — the fix would still call That(string?), not That<T>
await Assert.That(x).IsEqualTo("Blah");

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:

  • st8vsblobprodcus339.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/Meziantou.Analyzer.slnx --packages /tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/Meziantou.Analyzer.Test.csproj --packages /tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)

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

Copilot AI linked an issue Apr 24, 2026 that may be closed by this pull request
…le same-named overload

Agent-Logs-Url: https://github.com/meziantou/Meziantou.Analyzer/sessions/771fa8ce-1473-48ab-acdf-f27908b008c1

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix false positive of MA0042 in version 3.0.41 Fix MA0042 false positive when non-generic method has a same-named generic overload with awaitable return type Apr 24, 2026
Copilot AI requested a review from meziantou April 24, 2026 13:03
@meziantou meziantou marked this pull request as ready for review April 24, 2026 20:37
@meziantou meziantou merged commit a42144e into main Apr 24, 2026
13 checks passed
@meziantou meziantou deleted the copilot/fix-false-positive-ma0042 branch April 24, 2026 20:37
This was referenced Apr 24, 2026
IhateTrains pushed a commit to ParadoxGameConverters/ImperatorToCK3 that referenced this pull request Apr 26, 2026
Updated
[Meziantou.Analyzer](https://github.com/meziantou/Meziantou.Analyzer)
from 3.0.52 to 3.0.54.

<details>
<summary>Release notes</summary>

_Sourced from [Meziantou.Analyzer's
releases](https://github.com/meziantou/Meziantou.Analyzer/releases)._

## 3.0.54

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.54>

## What's Changed
* Add MA0193 Round mode analyzer and code fixes by @​meziantou in
meziantou/Meziantou.Analyzer#1104


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.53...3.0.54

## 3.0.53

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.53>

## What's Changed
* Fix MA0042 false positive when non-generic method has a same-named
generic overload with awaitable return type by @​Copilot in
meziantou/Meziantou.Analyzer#1102


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.52...3.0.53

Commits viewable in [compare
view](meziantou/Meziantou.Analyzer@3.0.52...3.0.54).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=Meziantou.Analyzer&package-manager=nuget&previous-version=3.0.52&new-version=3.0.54)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

3.0.41 introduces false positive of MA0042

2 participants