Skip to content

Wrong parameter in METGEN004 ErrorInvalidMethodReturnType diagnostic #6719

@KalleOlaviNiemitalo

Description

@KalleOlaviNiemitalo

Description

The METGEN004 diagnostic (ErrorInvalidMethodReturnType) claims to name the return type, but instead shows the name of the partial method.

Reproduction Steps

MetricsDemo.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" Version="9.8.0" />
  </ItemGroup>

</Project>

MetricsDemo.cs:

using System.Diagnostics.Metrics;
using Microsoft.Extensions.Diagnostics.Metrics;

namespace MetricsDemo
{
    internal partial class Instruments
    {
        [Counter("RequestName")]
        private static partial RequestCounter CreateRequestCounter(Meter meter);
    }

    internal partial class RequestCounter
    {
    }
}

Expected behavior

error METGEN004: Metric methods must not return 'RequestCounter' type (https://aka.ms/dotnet-extensions-warnings/METGEN004)

or

error METGEN004: Metric method 'CreateRequestCounter' must not return 'RequestCounter' type (https://aka.ms/dotnet-extensions-warnings/METGEN004)

Preferably with the declaration of the partial method as an additional location.

Actual behavior

error METGEN004: Metric methods must not return 'CreateRequestCounter' type (https://aka.ms/dotnet-extensions-warnings/METGEN004)

which mentions 'CreateRequestCounter' as if it were the name of a type, but it's actually the name of the partial method.

Regression?

No response

Known Workarounds

Use the attribute correctly.

Configuration

No response

Other information

The format string of the diagnostic message uses {0} as the return type:

<data name="ErrorInvalidMethodReturnTypeMessage" xml:space="preserve">
<value>Metric methods must not return '{0}' type</value>
</data>

Instead, {0} is the name of the partial method:

// Make sure return type is not from existing known type
Diag(DiagDescriptors.ErrorInvalidMethodReturnType, methodSymbol.ReturnType.GetLocation(), methodSymbol.Name);

Metadata

Metadata

Assignees

Labels

area-telemetrybugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions