Skip to content

Improve MA0042 async overload matching coverage#1065

Merged
meziantou merged 15 commits intomainfrom
pr1061-overload-tests
Mar 31, 2026
Merged

Improve MA0042 async overload matching coverage#1065
meziantou merged 15 commits intomainfrom
pr1061-overload-tests

Conversation

@meziantou
Copy link
Copy Markdown
Owner

Why

MA0042 overload detection was missing several realistic call-shape scenarios (generic inference, extension-vs-instance matching, ref-kind nuances, and numeric conversion compatibility), which caused false negatives in async context diagnostics.

What changed

  • Expanded overload matching in OverloadFinder to better support:
    • method-generic inference/constraint validation during comparison
    • ref/out strictness with in/by-value compatibility
    • extension/instance comparable parameter normalization
    • safer implicit numeric conversion compatibility (including System.Half -> float/double)
  • Improved async-equivalent candidate discovery in DoNotUseBlockingCallInAsyncContextAnalyzer by broadening lookup around receiver/type context.
  • Added a large async-context test matrix covering:
    • generic compatibility (positive and negative)
    • numeric widening/narrowing and floating-point precision-sensitive cases
    • instance vs extension method matching
    • argument forms (default, null, named args) and modifier edge cases
  • Updated DocumentationGenerator to use FindRequiredGitRepositoryRoot() and bumped Meziantou.Framework.FullPath to 1.1.18.

Validation

  • dotnet test --filter "FullyQualifiedName~DoNotUseBlockingCallInAsyncContextAnalyzer_AsyncContextTests.Argument_ImplicitNumeric" (passed)
  • dotnet test --filter "FullyQualifiedName~DoNotUseBlockingCallInAsyncContextAnalyzer_AsyncContextTests" (passed)
  • dotnet run --project src/DocumentationGenerator currently fails in this worktree with Cannot find the current git folder in this environment; code was updated toward required FullPath usage.

meziantou and others added 3 commits March 29, 2026 13:17
Expand MA0042 overload matching to better handle generic inference, ref-kind compatibility, instance/extension lookup, and safe numeric conversions. Add a broad async-context test matrix covering generic compatibility, numeric conversion scenarios, and edge cases such as long->double and Half conversions.

Also update DocumentationGenerator to use FindRequiredGitRepositoryRoot() and bump Meziantou.Framework.FullPath to 1.1.18.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace collection-expression returns with ImmutableArray.CreateRange/Empty in MA0042 overload lookup paths so builds succeed with older Roslyn toolsets used in CI.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add MA0042 tests for same generic constraints (diagnostic) and different generic constraint order (no diagnostic), and keep coverage for different type-parameter mapping with the same generic definition.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Adjust MA0042 overload matching so List<T> to IEnumerable<T> generic overloads are not treated as equivalent async replacements. Update tests accordingly by asserting no diagnostic for this case.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

class Test
{
public void A(in int value) => throw null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

may be there should be separate analyzer without fix for in, ref and out, because modifiers are forbidden for async methods but you can rewrite it to async version

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The code should only accept the in modifier. out and ref would be incompatible. While it's an invalid modifier when you define an async method, an async method can use a method with a in parameter.

As the use-case is to replace method invocation, I think this is ok.

Task Foo(in int a) => throw null!;

async Task Bar(int a)
{
    await Foo(a); // Valid
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this sounds reasonable

But I'm talking about other case

bool TryA(int b, out Foo f) => throw null!;
Task(bool, Foo)> TryAAsync(int b) => throw null!;

I understand that it's a long shot for analyzer to guess this type of overload, but it can be usefull :)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

It would have more impact that just finding the overload. The code fix must be aware of it, not just using another method name, but assigning variables (it can be tricky depending on the context).

meziantou and others added 10 commits March 30, 2026 20:06
Replace namespace string comparisons in OverloadFinder with assembly metadata symbol resolution for IEnumerable<T> and Half detection.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
# Conflicts:
#	src/DocumentationGenerator/Program.cs
Rename GenericArgument_NestedCompatibleGenericDefinitions_ShouldReport to GenericArgument_CompatibleGenericDefinitions_ShouldReport for clearer intent after merge.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add OverloadOptions switches to disable numeric conversions, params/non-params compatibility, in-modifier compatibility, and interface conversions. Wire these options through OverloadFinder matching and MA0042 call-site checks.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Flip newly added OverloadOptions toggles from Disable* to Allow* semantics with default true values, and update OverloadFinder checks accordingly.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meziantou meziantou marked this pull request as ready for review March 31, 2026 18:49
This was referenced Apr 13, 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.

2 participants