dotnet / roslyn-analyzers

MIT License
1.59k stars 466 forks source link

CA1812 flags non-static class implementing interface with static abstract member and used as type argument #6218

Open alexrp opened 2 years ago

alexrp commented 2 years ago

Analyzer

Diagnostic ID: CA1812: Avoid uninstantiated internal classes

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 7.0.100-rc.2.22477.23

Describe the bug

A non-static class that implements an interface with static abstract members and is used as a type argument gets flagged by CA1812.

Steps To Reproduce

public interface IProgram {
    public static abstract Task RunAsync();
}

public static class ProgramHost {
    public static async Task RunAsync<TProgram>()
        where TProgram : IProgram {
        return TProgram.RunAsync();
    }
}

// This triggers CA1812.
internal sealed class Program : IProgram {
    public static async Task RunAsync() {
    }
}

internal static class GeneratedProgram {
    private static Task Main() {
        return ProgramHost.RunAsync<Program>();
    }
}

Expected behavior

Program should not be flagged by CA1812.

Actual behavior

error CA1812: 'Program' is an internal class that is apparen tly never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it 'static ' (Module in Visual Basic).

Evangelink commented 2 years ago

Hey @alexrp! Thanks for flagging this false positive.