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

Allow declaring DiagnosticAnalyzerAttributes on abstract classes without causing initialization errors #56340

Open
Rekkonnect opened this issue Sep 11, 2021 · 0 comments
Assignees
Labels
Area-Analyzers Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request
Milestone

Comments

@Rekkonnect
Copy link
Contributor

Rekkonnect commented Sep 11, 2021

Background and Motivation

DiagnosticAnalyzerAttribute is inheritable by deriving classes, meaning that abstract classes can make use of it, however Roslyn will attempt to initialize new instances of abstract classes. This will result in AD0001 warnings cryptically indicating that an instance of an abstract class could not be created.

Usage Examples

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public abstract class BaseAnalyzer : DiagnosticAnalyzer
{
    // some abstracted behavior
}

public sealed class DerivedAnalyzer : BaseAnalyzer
{
}

public sealed class AnotherAnalyzer : BaseAnalyzer
{
}

Roslyn will attempt to initialize an instance of DerivedAnalyzer and another of AnotherAnalyzer, but not BaseAnalyzer, because it's abstract. Both instances of the analyzers will be considered to be analyzers for C#.

Solutions

From what I've been looking at, it's this LINQ expression to blame for this. Maybe an additional check for whether the referened type is abstract would deal with this problem.

Risks

Code that would otherwise throw an exception and cause an AD0001 warning, will no longer do. It's a really sad day for badly written analyzers.

@Rekkonnect Rekkonnect added Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request labels Sep 11, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Analyzers untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 11, 2021
@jinujoseph jinujoseph removed the untriaged Issues and PRs which have not yet been triaged by a lead label Sep 15, 2021
@jinujoseph jinujoseph added this to the Backlog milestone Sep 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Analyzers Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request
Projects
None yet
Development

No branches or pull requests

3 participants