dotnet / roslyn-analyzers

MIT License
1.58k stars 463 forks source link

BannedApiAnalyzers: allow to ban a type when used as a base class #7100

Open NinjaCross opened 9 months ago

NinjaCross commented 9 months ago

Analyzer

Diagnostic ID: RS0030

Describe the improvement

The analyzer is currently lacking the possibility to ban a type when used as a base class. This is usefull when in a system the developers should not inherit from a specific base class (because i.e. there is a better strategy to obtain the same purpose), but such base class:

Describe suggestions on how to achieve the rule

The syntax can be the same already used for banning types, but instead of something like this:

T:Microsoft.AspNetCore.SignalR.Hub; blah blah blah

use a different prefix, like BT

BT:Microsoft.AspNetCore.SignalR.Hub; blah blah blah

Additional context

This idea came to me when in a project I was working on, the "Hub" class mustn't be used to inherit new SignalR Hubs (because only one "global" SignalR Hub was allowed into the system, and new functionalities had to be implemented into such "global" SignalR Hub instead of distributing them into new distinct SignalR Hub classes)

sharwell commented 9 months ago

I believe I would recommend making a new analyzer for this purpose, since it would be fairly easy to implement.