Roslyn: Provide an option to the diagnostic suppression codefix to use SuppressMessage vs. pragmas

Created on 17 Feb 2017  路  14Comments  路  Source: dotnet/roslyn

I understand the rationale behind adopting pragmas as the defacto suppression codefix as discussed in this issue, but mixing pragmas and [SuppressMessage] in large, existing codebases is non-ideal. It would be nice if there was an option/configuration file that could tweak the suppression mechanism the codefix employs.

Area-IDE Feature Request IDE-CodeStyle Resolution-Fixed

Most helpful comment

Wanted to bump this a bit. Anxiously awaiting the ability to in-source suppress via [SuppressMessage] without having to globally suppress the issue then cut/paste the suppression into the source.

Our use case is that we don't allow our developers to suppress anything without a valid justification that can be evaluated at code review time.

All 14 comments

+1 A global option or perhaps better, add a choice to the right click menu in the error list so you can use [SuppressMessage] or #pragma. Former allows suppression without interfering with the code and documentation via the Justification parameter. Latter allows more fine grained control.

I would prefer the SuppressMessageAttribute inside the source code file too and it shouldn't be an issue to add an option for Suppress > In Source with pragma AND Suppress > In Source with attribute.

Wanted to bump this a bit. Anxiously awaiting the ability to in-source suppress via [SuppressMessage] without having to globally suppress the issue then cut/paste the suppression into the source.

Our use case is that we don't allow our developers to suppress anything without a valid justification that can be evaluated at code review time.

I guess the solution/request is purely related to the IDE and enhancing the right click menu to bring back the previous MS Code Analysis behaviour. Adding the [SuppressMessage] attribute by hand, while not ideal, does work.

@gafter Perhaps the label should be changed to Area-IDE?

This code action was considered during the initial implementation of the suppression code fix provider. However, it was felt that this was adding too many alternatives to do the same operation. It was decided that we will provide one fix to suppress "inline" in source using pragmas (which would also be applicable to compiler diagnostics) and one fix to suppress in source adding suppress message attributes to a global suppressions file that does not pollute the current source file. As a workaround for local suppress message attribute suppression, which is uncommon, user can generate the global one, then cut and paste the generated attribute to the desired symbol and remove the Target and Scope strings in the attribute.

We have a workaround which was considered acceptable at the time of implementation, given the assumption about less frequent usage of locally applied suppress message attribute.

Tagging @sharwell, we should discuss this feature request for 16.1 at the next design meeting - I believe implementing this wouldn't be too difficult given we already have a code action that generates a similar global suppress message attribute with more fields then needed here.

Design review conclusion: we would accept a pull request adding this feature to the same submenu that already includes the ability to suppress using GlobalSuppressions or #pragma.

IIRC, this feature was implemented long ago, so source control history _may_ reveal code that can be used for the implementation. However, it may be so old that it's not relevant anymore.

I found the last trace of the local suppression at this time in the history:
https://github.com/dotnet/roslyn/tree/4dfefe7b49c33d1d078494d52a84b16772158b11

I've brought that implementation (with some adaptations) into a fork of the master branch. It's working, however there are two things still to be done:

  • create unit tests (existing unit tests are not affected by the changes)
  • translate the title for the new suppression

Here you can take a look at the changes:
https://github.com/mmihaly/roslyn/commit/dc3733fd582414524716fd25ad462d7a424ba5d7

This is the first time I'm using/contributing to github, all comments are welcome. I'd also appreciate some help with the unit tests. And I have no clue what to do with the translations.

I'm wondering whether I can create a PR with the current state or first these TODO items need to be completed.

I'm wondering whether I can create a PR with the current state

This would be the easiest approach 馃憤

Implemented with https://github.com/dotnet/roslyn/pull/35327 by @mmihaly. Thanks a lot for your contribution! The feature should be available when Dev16 Update2 (16.2) is released.

You're welcome! And thanks letting this feature back, it will help our dev team keep suppressions consistent throughout our considerably large solution!

I'll create another PR with support for the 'fix all' feature as soon as I have a working version.

Hi, I'm using 16.2.1 but I don't see this feature? I'm acting on an IDE rule if that helps. IDE0067

Edit. The option appears in the "light bulb" quick actions menu. However, it does not appear in the Error List right click menu options. I guess it should for a) completeness b) to avoid confusion.

@Gav-Brown can you please open a new issue for it?

Was this page helpful?
0 / 5 - 0 ratings