Getting IDE0079 ("Remove unnecessary suppression") to play well with NDepend suppressions?

42 Views Asked by At

Sometimes I'll have an NDepend rule that I want to keep enabled in general, but want to disable for a specific case. So, I'll suppress it as in the following example:

    [SuppressMessage(
       "NDepend",
       "ND1212:AvoidEmptyInterfaces",
       Justification = "We don't need no stinkin' justification")]
    public interface IPurposefullyEmptyInterface;

But doing so will cause an IDE0079 warning, "Remove unnecessary suppression". Just like ND1212, I don't want to turn off IDE0079 in general either, but I do want to turn it off in this specific instance. So:

#pragma warning disable IDE0079 // Remove unnecessary suppression
    [SuppressMessage(
       "NDepend",
       "ND1212:AvoidEmptyInterfaces",
       Justification = "We don't need no stinkin' justification")]
#pragma warning restore IDE0079 // Remove unnecessary suppression
    public interface IPurposefullyEmptyInterface;

But it seems like I should not need to do that second step at all; the ND1212 suppression is not unnecessary, so I shouldn't be getting the IDE0079 warning in the first place. I guess that whatever is responsible for producing IDE0079 (uhhhh... presumably the IDE, i.e. VS Community 2022? Or maybe one of the analyzers I've installed) does not play well with NDepend suppressions (ND1212 is just an example - this happens with other ND* messages too).

Is there a way to make this "suppress the message saying to stop suppressing the other message" problem stop happening (while keeping both the ND* messages and the IDE0079 message turned on in general)?

For example, I thought that maybe doing something like this at one single place in my project would take care of it:

[SuppressMessage(
   "CodeQuality",
   "IDE0079:Remove unnecessary suppression",
   TargetCategory="NDepend",
   Justification="I am the law")]

Unfortunately, looking at the docs for SuppressMessageAttribute, I don't see anything that seems to allow for this sort of idea.

1

There are 1 best solutions below

1
Julie On

You can add this to your .editorconfig :

dotnet_remove_unnecessary_suppression_exclusions = category: NDepend

This will exclude all your [SuppressMessage("NDepend",...)] from the IDE0079 rule analysis.
See https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0079 for more information.