roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Create [WarningAttribute] and [ErrorAttribute] that triggers compiler warnings/errors the way [ObsoleteAttribute] does.

Open TwoRedCells opened this issue 11 years ago • 12 comments

FEATUREREQUEST COMPILER

Add a [WarningAttribute] or [CompilerWarningAttribute] and [CompilerErrorAttribute] that works the same way as [ObsoleteAttribute]; it would trigger a compiler warning or error with a customized message. Ideally it would not be sealed. You could do things like:

[Warning("This class uses PInvoke and will prevent ARM compatibility.")]

[Experimental("This method has not completed Quality Assurance, and should not be used in a production environment.")] 

This feature would be particularly useful for component creators.

This would be the bees' knees.

[WarningAttribute] on UserVoice

TwoRedCells avatar Jan 29 '15 14:01 TwoRedCells

Can this not be accomplished today using a custom StyleCop or FXCop rule? Is there something lacking in that extensibility point?

aolszowka avatar Jan 30 '15 15:01 aolszowka

I don't personally use those tools, so I'm not sure. The spirit of the suggestion is that if someone uses a component that I built, they would (for example) be warned when they use a certain method that they are sacrificing portability. This would not rely on the chance that they have a third-party tool installed.

TwoRedCells avatar Jan 30 '15 15:01 TwoRedCells

Understandable, more often than not in practice I notice that warnings (such as [Obsolete]) go ignored by developers as they just consider it "noise". I mention FXCop (https://msdn.microsoft.com/en-us/library/dd264939.aspx) as this particular scenario would seem to be a good candidate for a Portability Ruleset (in fact there is a similarly defined error already CA1901: P/Invoke declarations should be portable this is included in the AllRules.ruleset https://msdn.microsoft.com/en-us/library/dd264925.aspx).

It seems as though this feature is a desire to add more explicit contracts in regards to its usage beyond documentation to try and alert developers within the IDE (the intent of FXCop/Code Analysis). Code Analysis ships with VS 2012 Professional (I have not looked at VS2013 Professional or even Community edition yet) this is not a third party tool.

aolszowka avatar Jan 30 '15 16:01 aolszowka

Understandable, more often than not in practice I notice that warnings (such as [Obsolete]) go ignored by developers as they just consider it "noise".

Good point. That's part of the issue I'm trying to address. Attributes subclassing [CompilerErrorAttribute] simply would not compile. Warnings of course could be ignored (at their peril). I'm one of those jerks who forces my programmers in submission by checking Treat errors as warnings, and enabling XML comments. :)

TwoRedCells avatar Jan 30 '15 16:01 TwoRedCells

The way we chose to address this is to write Analyzers. Analyzers plug into the compiler and can report warnings or errors to the user (they are treated just like any other compiler warning or error so turning on warnings-as-errors works too). For example here is one of our Analyzers that reports warnings for not serializing objects correctly. You can also take a look at CodeCracker or StyleCop for some more examples. In your case you could include some analyzers that warn someone whenever they use your api incorrectly, but "incorrectly" can mean much more than just using deprecated methods.

jmarolf avatar Jan 30 '15 18:01 jmarolf

I don't think we would want to put this in the compiler given the analyzer extensibility mechanism is well suited to do exactly this kind of analysis.

srivatsn avatar Feb 28 '15 17:02 srivatsn

Thank you for your consideration and introduction to Analyzers and some other excellent dev tools.

TwoRedCells avatar Apr 16 '15 13:04 TwoRedCells

In 2020, given analyzers and FxCop have been deprecated and are generally a mess when porting .NET Framework code to .NET Core code, what is the recommended way today to do what OP was trying to achieve in this post 5 years ago? I'm talking about .NET Core, obviously.

I'd like to write a library to issue custom info or warning messages when my library is incorrectly used. So this scenario doesn't really fit into either the obsolete category nor style analysis category, because my code is neither obsolete nor is it an issue with a coding style. It really is business logic plugged into compiler to help developers navigate the usage of my library, aka make my library as user friendly and as easy to learn as possible.

agaace avatar Jul 11 '20 23:07 agaace

Analyzers are not deprecated and are the recommended way to accomplish this use case. for example, the xUnit testing framework ships its analyzers from here. Would like to understand where its been said that analyzers are deprecated (FxCop is being depricated, but that is because it is being replaced by analyzers).

jmarolf avatar Jul 12 '20 21:07 jmarolf

I too am interested to hear that Analyzers has been deprecated. We use StyleCop Analzyers and other Analyzers pretty heavily (with their code fixes) including some in house ones. Do you have a link to a blog post or announcement where they have been deprecated?

aolszowka avatar Jul 12 '20 23:07 aolszowka

I am here to say, as someone actively working on analyzers for Microsoft, that they are not deprecated. Just trying to understand what was said to give this impression out there in the blog-o-sphere.

jmarolf avatar Jul 13 '20 15:07 jmarolf

This feature will definetly be useful for situations where you create third party libraries that is given to other developers, and you want to show a custom message. In this situation, you cannot force the developer that consumes the library to setup a custom code analyzer.

Here is a real-life scenario where the custom message attribute (similar to ObsoleteAttribute) would have been meaningful. We provide a library to other developers distrubuted as a dll file. This dll uses another third party library. For the newer version of our library we want to replace the other third party assemblies with another vendor's library. We want to give the users at least one release of time to remove the old third party library and install the new one in their projects. However, currenty there are no means to do that in C#. An attribute that is showing a custom message in the compile/design-time would be very helpful here.

The scenario is a bit similar to one described above by @agaace

martinivanoff avatar Aug 13 '24 11:08 martinivanoff