-
Notifications
You must be signed in to change notification settings - Fork 840
Description
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:
extensions/src/Generators/Microsoft.Gen.Metrics/Resources.resx
Lines 168 to 170 in 02dcda1
| <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:
extensions/src/Generators/Microsoft.Gen.Metrics/Parser.cs
Lines 576 to 577 in 02dcda1
| // Make sure return type is not from existing known type | |
| Diag(DiagDescriptors.ErrorInvalidMethodReturnType, methodSymbol.ReturnType.GetLocation(), methodSymbol.Name); |