Trying to suppress StyleCop message SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine
I am trying to suppress the following StyleCop message for a specific property:
SA1513: Statements or elements wrapped in curly brackets must be followed by a blank line.
I am trying to do the following, but it doesn't seem to work:
[SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine", Justification = "There are no issues with this code")]
public string CustomerId
{开发者_Go百科
get
{
return this.GetProperty(CustomerIdProperty);
}
set
{
if (this.IsNew)
{
this.SetProperty(CustomerIdProperty, value);
}
else
{
throw new ReadOnlyException("Id value can only be changed for a new record.");
}
}
}
Am I just doing something wrong? Or is this just not possible? It's a good rule, just not valid in my case for a property.
Update
Tried switching from DocumentationRules to LayoutRules ... still not suppressing.
[DataObjectField(true, false)]
[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine", Justification = "There are no issues with this code")]
public string CustomerId
{
get
{
return this.GetProperty(CustomerIdProperty);
}
set
{
if (this.IsNew)
{
this.SetProperty(CustomerIdProperty, value);
}
else
{
throw new ReadOnlyException("Id value can only be changed for a new record.");
}
}
}
I think this might be a problem with StyleCop. Which version do you have installed? This page states that:
Starting with StyleCop 4.3.2, it is possible to suppress the reporting of rule violations by adding suppression attributes within the source code.
I've just found that I can't suppress any messages. The installer I used just gives the version as 4.3. The latest version on the Codeplex is 4.4.0.0. Make sure you have that version installed.
Update
I've been doing some checking and I can suppress DocumentationRules:
[SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules",
"SA1600:ElementsMustBeDocumented",
Justification = "Reviewed. Suppression is OK here.")]
but not SpacingRules or LayoutRules. However, nothing I've found indicates why this should be the case.
Your suppression uses Microsoft.StyleCop.CSharp.DocumentationRules
. I think it should be Microsoft.StyleCop.CSharp.LayoutRules
.
[SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine", Justification = "There are no issues with this code")]
works in latest StyleCop. Just removed "Microsoft." prefix.
There is a bug in StyleCop that let's you only suppress certain kinds of rules. This will be fixed in StyleCop 4.4, which is due to be released soon.
Be careful to read the StyleCop documentation to figure-out how to suppress a rule. The following worked in my code:
[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules",
"SA1402:FileMayOnlyContainASingleClass",
Justification = "Splitting this file into classes would get too confusing.")]
From the help file:
The SuppressMessage attribute has the following format:
[SuppressMessage("Rule Category, "Rule Id", "Justification")]
Where:
Rule Category - The StyleCop rule namespace in which the rule is defined. For example, Microsoft.StyleCop.CSharp.DocumentationRules
Rule Id -The identifier for the rule, using the format shortname:longname.
For example, SA1600:ElementsMustBeDocumented
Justification - The text that is used to document the reason for suppressing the message.
And as already mentioned, make sure you are referencing the correct rules namespace.
Just put a blank line between your get block and your set block.
That's all you have to do, add one blank line and the problem is solved.
精彩评论