Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA1003 does not take the return value of an event handler into account #7373

Open
dlebansais opened this issue Aug 9, 2024 · 1 comment
Open
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers False_Positive A diagnostic is reported for non-problematic case help wanted The issue is up-for-grabs, and can be claimed by commenting

Comments

@dlebansais
Copy link

Version Used: 17.10.0

Steps to Reproduce:

  1. Create a .NET 8 library project with a class containing a ResolveEventHandler event handler (see sample code below).
  2. Set the analysis level to lastest in the project with <AnalysisLevel>latest-all</AnalysisLevel>.
  3. Compile.
namespace Test;

using System;

public class TestClass
{
    public event ResolveEventHandler? AssemblyResolve; // CA1003 cannot be fixed here
}

Diagnostic Id: CA1003: Change the event 'AssemblyResolve' to replace the type 'System.ResolveEventHandler?' with a generic EventHandler, for example EventHandler, where T is a valid EventArgs

Expected Behavior: CA1003 is not reported, because no generic EventHandler can replace the type 'System.ResolveEventHandler?'.

Actual Behavior: CA1003 is reported.

The definition of System.ResolveEventHandler is:

#region Assembly System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.6\ref\net8.0\System.Runtime.dll
#endregion

#nullable enable

using System.Reflection;

namespace System
{
    //
    // Summary:
    //     Represents a method that handles the System.AppDomain.TypeResolve, System.AppDomain.ResourceResolve,
    //     or System.AppDomain.AssemblyResolve event of an System.AppDomain.
    //
    // Parameters:
    //   sender:
    //     The source of the event.
    //
    //   args:
    //     The event data.
    //
    // Returns:
    //     The assembly that resolves the type, assembly, or resource; or null if the assembly
    //     cannot be resolved.
    public delegate Assembly? ResolveEventHandler(object? sender, ResolveEventArgs args);
}

This event handler returns a Assembly? value, but all variations of EventHandler (generic or not) don't return a value and therefore cannot be used.

Possible solutions:

  • Add a new EventHandler<TEventArgs, TEventResult> type to .NET.
  • Change ResolveEventArgs to add a new property public Assembly? Result { get; set; } and the set the return type to void (this is a breaking change).
  • Do not report CA1003 for events whose type is a delegate that returns a value.
@huoyaoyuan
Copy link
Member

CAxxxx analyzers belong to https://github.com/dotnet/roslyn-analyzers repo.

@jjonescz jjonescz transferred this issue from dotnet/roslyn Aug 13, 2024
@buyaa-n buyaa-n added Area-Microsoft.CodeAnalysis.NetAnalyzers False_Positive A diagnostic is reported for non-problematic case help wanted The issue is up-for-grabs, and can be claimed by commenting and removed untriaged labels Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers False_Positive A diagnostic is reported for non-problematic case help wanted The issue is up-for-grabs, and can be claimed by commenting
Projects
None yet
Development

No branches or pull requests

3 participants