简体   繁体   中英

How to suppress StyleCop Warning SA1649

I am trying to suppress two occurences of the SA1649 error in StyleCop.

According to the documentation the suppression attribute looks like the attribute below

[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:FileHeaderFileNameDocumentationMustMatchTypeName", Justification = "Reviewed.")]

However, I cannot figure out what element I should apply the attribute to. If I put the attribute on the first element of the file (the IClassFactory Interface), I get the following error:

SA1649 : CSharp.Documentation : The file attribute in the file header's copyright tag must contain the name of the first type in the file and can be any of these: "IClassFactory" C:(...)COMHelper.cs

Can that error be suppressed? What element should it be applied to?

Use a namespace level suppression:

[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:FileHeaderFileNameDocumentationMustMatchTypeName", Justification = "Reviewed.")]

namespace MyNamespace
{

}

The suppress attributes should be placed on the class or interface declaration. This should be done in each cs file that you don't want this rule to be applied to.

However, if you want to suppress this for all your cs files in your project then you should consider disabling this rule all together in the settings. That way you don't have a suppress rule declaration on every class file.

Chris' answer works, but to add to it you may have to suppress the file header rule too.

[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:FileHeaderFileNameDocumentationMustMatchTypeName", Justification = "Reviewed.")]
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:FileMustHaveHeader", Justification = "Reviewed.")]

//----------------------------------------------------------------------------------------------------
// <copyright file="FileName.cs" company="Company">
//     Copyright (c) Company. All rights reserved.
// </copyright>
// <author>n00bz</author>
//----------------------------------------------------------------------------------------------------
namespace TestNamespace
{
}

Better yet:

//----------------------------------------------------------------------------------------------------
// <copyright file="FileName.cs" company="Company">
//     Copyright (c) Company. All rights reserved.
// </copyright>
// <author>n00bz</author>
//----------------------------------------------------------------------------------------------------
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:FileHeaderFileNameDocumentationMustMatchTypeName", Justification = "Reviewed.")]

namespace TestNamespace
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM