Search code examples
c#.netnamingsuppress-warningsroslyn-code-analysis

How to suppress warning for all derived types?


I have an ITemplate interface in my project. All interfaces derived from it have names that do not start with I. This causes the InconsistentNaming warning to appear.

I want to suppress this warning for all interfaces derived from the ITemplate interface.

I know about SuppressMessage attribute, but it does not suppress warnings on the derived types. I tried to play around with Scope property of this attribute, but unfortunately nothing changed.

Basically, I want to do something like this:

[SuppressMessage("ReSharper", "InconsistentNaming", Target = Targets.Inheritors)]
public interface ITemplate;

Solution

  • There is no way to suppress the Inconsistent Naming warning only for types deriving from a specific interface. There are a couple of other options.

    pragma IDE1006

    Use the pragma directive to suppress the warning within in an entire file. Place the following at the top of the file:

    #pragma warning disable IDE1006 // Inconsistent naming
    

    To re-enable the warning anywhere else in the file, add the following just above where the warning should be re-enabled:

    #pragma warning restore IDE1006 // Inconsistent naming
    

    .csproj NoWarn

    To suppress the warning for an entire project, use the NoWarn property. Open up the .csproj in question and add the following:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <!-- Other properties -->
        <NoWarn>$(NoWarn);IDE1006</NoWarn>
      </PropertyGroup>
    
      <!-- Other project settings -->
    
    </Project>