I am trying to suppress "Missing XML comment" (CS1591) but only for a specific class. The class in my case is System.Windows.DependencyProperty. I want the warnings to appear for every other occasion. Adding CS1591 to <NoWarn> won't work as this will simply ignore all occurrences.
What I tried was to create a GlobalSuppressions.cs at my project's root and adding the following line to it:
[assembly: SuppressMessage( "Missing XML comment", "CS1591", Justification = "No need for DependencyProperty docs.", Scope = "type", Target = "~T:System.Windows.DependencyProperty" )]
Unfortunately this doesn't work at all.
You should be able to use #pragma warning disable CS1591 right above your class definition and #pragma warning restore CS1591 at the end.
see: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-pragma-warning
I have a class with a namespace not matching the folder structure of the project.
With // ReSharper disable once CheckNamespace I can disable the hint that the namespace does not correspond to the file-location. But when Refactoring the Projects namespaces, the class is still selected for adjusting the namespace.
Is there a way to disable the adjust namespace?
Update with sample:
In my case I have a view class that has to be in the System.Web.Mvc-Namespace.
The file location is [Root]>src>Views
The default namespace of the Root-Project is Com.xxxxx.Commons.Web
So Resharper always wants to adjust the namespace to either
Com.xxxxx.Commons.Web.Views or to Com.xxxxx.Commons.Web if I disable the namespace-provider-flag für the Views folder.
Unfortunately it's a long existing bug in R# that disable CheckNamespace isn't respected by the "Adjust namespaces" refactoring. As I know, there is no other way to exclude specific files from this refactoring.
You can disable the folder as a Namespace Provider by opening the properties window (right click -> properties) for the folder you wish to exclude and change Namespace Provider to false.
ReSharper's Adjust Namespace refactoring no longer affects namespaces marked with // ReSharper disable CheckNamespace. The long-existing bug in ReSharper (link referenced in #ulrichb's old answer) has been fixed in ReSharper 2016.2.
Limitations (currently, as of ReSharper 2018.1)
If there is no namespace at all, ReSharper will automatically add a namespace even if disable CheckNamespace is specified for that file.
For example, this works:
// ReSharper disable once CheckNamespace
namespace DoNotAdjustThisNamespace
{
public class TestClass1
{
}
}
However, this does not:
// ReSharper disable once CheckNamespace
public class TestClass1
{
}
There is still a misleading UI issue where all files still appear in the Adjust Namespace UI, but the namespace changes will not actually take place. This is a bit misleading..
I am using PostSharp in an Outlook Plugin app. If I add the following attribute to a class in my project it logs properly:
namespace Foo.Bar
{
[Log(AttributeTargetMemberAttributes = MulticastAttributes.Public)]
public class FooBar {...}
}
What I really want to do is log everything in the Foo.* namespace. I tried using the addin in VS which created a globalaspects.cs and updated my project.pssln file. At this point it wont build with the following error msg:
.dll uses non-licensed features (PostSharp Professional). Please enter a valid license key.
I figured it was recursing on itself so I added an AttributeExclude = true in the assembly line that was generated for me. It now looks like this (in globalaspects.cs):
[assembly: Log(AttributeExclude = true, AttributeTargetTypes = "Foo.*", AttributeTargetTypeAttributes = MulticastAttributes.Public, AttributeTargetMemberAttributes = MulticastAttributes.Public)]
No luck, it doesn't log anything this way. Any ideas why?
Additional info:
I am logging to log4net and I have other logging code that is working (it also works at both the class and method levels with PostSharp).
According to this page free license of PostSharp currently has limitation on number of methods on which [Log] attribute is applied. In my opinion, you have exceeded this number by applying the aspect on the whole namespace.
AttributeExclude means that the attribute will not be applied on declarations that satisfy conditions set in this attribute. It is basically set inclusion/exclusion operation. For example you can include Namespace1, exclude Namespace1.Namespace2 and again include Namespace1.Namespace2.Namespace3.
Therefore the following would be correct:
[assembly: Log(AttributeTargetTypes = "Foo.*",
AttributeTargetTypeAttributes = MulticastAttributes.Public,
AttributeTargetMemberAttributes = MulticastAttributes.Public)]
For more information about attribute multicasting, you can take a look on this article.
Note to reviewers: I'm one of developers working on PostSharp. I'm aware that this answer touches licensing, which is behind the red line and I have tried my best not to cross it too much.
I am working in an application and developing few classes for demo purpose. I know that these classes will be removed in future.
Is it possible to ignore all the stylecop warnings for those classes as I dont want to spent the time on those warnings?
I searched but found that I can only ignore via settings in stylecop( this will effect other classes too) or to some specific rule ( I just want to ignore all warnings).
You can trick StyleCop into not processing a file at all by adding this header at the top:
//------------------------------------------------------------------------------
// <auto-generated>
// Well, not really. This is just a trick to get StyleCop off my back.
// </auto-generated>
//------------------------------------------------------------------------------
Beginning with StyleCop 4.4.0, it is also possible to suppress all of the rules within a rule namespace, using a single suppression attribute. This is indicated by replacing the rule CheckID and rule name with a single asterisk. The following code example suppresses all of StyleCop's default documentation rules within the inner class. In this case, StyleCop would still flag a violation indicating that the outer class is missing documentation, but it would ignore all documentation rules for the inner class and its contents.
public class OuterClass
{
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "*")]
public class InnerClass
{
public void MyUndocumentedMethod
{
}
}
}
http://stylecop.soyuz5.com/Suppressions.html
Thanks to Bartłomiej Mucha for the answer I've just used. As I discovered, the "*" works well for the specific rule, but you do have to add suppressions for each category. Here's the complete set - if you copy these into the top of a class, you should find that all StyleCop errors are suppressed:
[SuppressMessage("StyleCop.CSharp.NamingRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.LayoutRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.OrderingRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.SpacingRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "*", Justification = "Reviewed. Suppression is OK here.")]
internal class MyClass
{
// ...
}
If you want to prevent StyleCop from running over a file, you can mark it excluded in your .csproj file using the ExcludeFromStyleCop attribute mentioned at http://stylecop.codeplex.com/wikipage?title=Using%20StyleCop%20on%20Legacy%20Projects&referringTitle=Documentation.
You can suppress rules by adding attributes to blocks of code. Here's a simple example on a class from the blog post linked below, but you can do it on various members individually:
[SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented")]
public class MyUndocumentedClass
{
public void MyUndocumentedMethod {}
}
I'm using StyleCop and want to suppress some warning which does not suit my style. I prefer to have solution for
1) in-line code suppressing
2) global setting suppressing
I've searched the internet but still not sure how to do the suppressing.
For method 1), They said to add the lines:
[assembly: SuppressMessage("Microsoft.Design",
"SA1202:All private methods must be placed after all public methods",
Scope = "namespace", Target = "Consus.Client.ClientVaultModule.Services.OnlineDetection")]
But they do not say where and which namespace to be used.
For method 2), they said to use GlobalSuppress file but it seems not easy to search for a how-to do it at the moment.
Please help.
[Edited]
In my case, I have the warning about SA1202: All private methods must be placed after all public methods which is bothering since I group my related codes into regions. I want to suppress those warning for just some certain methods.
Here's what you need:
[SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess")]
An example of inline suppression would be similar to this - examine the namespaces in the code compared to the suppression
namespace Soapi
{
///<summary>
///</summary>
///<param name = "message"></param>
///<param name = "statusCode"></param>
///<param name = "innerException"></param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)")]
public ApiException(string message, ErrorCode statusCode, Exception innerException)
: base(String.Format("{0}\r\nStatusCode:{1}", message, statusCode), innerException)
{
this.statusCode = statusCode;
}
A global supression file is a file in the root of your project named GlobalSuppressions.cs and might look like this:
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
//
// To add a suppression to this file, right-click the message in the
// Error List, point to "Suppress Message(s)", and click
// "In Project Suppression File".
// You do not need to add suppressions to this file manually.
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object,System.Object)", Scope = "member", Target = "Soapi.ApiException.#.ctor(System.String,Soapi.ErrorCode,System.String,System.Exception)")]
And you can generate this code automatically by right-clicking on the warning.
Starting with StyleCop 4.3.2, it is possible to suppress the reporting of rule violations by adding suppression attributes within the source code.
Rule Suppressions
http://stylecop.soyuz5.com/Suppressions.html
but it says -
Global Suppressions
StyleCop does not support the notion of global suppressions or
file-level suppressions. Suppressions must be placed on a code
element.
If you've installed StyleCop, you can right-click your project and there will be a StyleCop option. Click this and you'll see you can prevent certain rules from even running against your project. Moreover, you can create a separate rules file to share between different projects. This means you can configure the rules once the way you want them and then share that configuration between all your projects.
For individual overrides, SuppressMessage is the way to go.
Go to Solution Explorer
Go to your project
Expand references
Expand Analyzers
Expand StyleCop.Analyzers
Right click on a particular rule which you want to disable at a global (project) level
Set Rule Set severity -> Select None
Read the admonition from Style Cop, looking for the alphanumeric code. In your case 'SA1202'. Then browse to the corresponding page on the Style Cop website. Change the URL as appropriate https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md
Copy the line labelled 'How to Suppress Violations'. Paste the attribute above the class about which Style Cop moans
[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "Reviewed.")]
Cant you just remove the rule instead of soiling your code?
Same goes for FxCop...
1.
In your case, correct SuppressMessage attribute should like like the following:
[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess")]
private void SomeMethod()
{
}
Note that you can place it on any other element (e.g, on the class - then all similar violations in the entire class will be supressed).
I also agree that it's quite unobvious what to write in these fields.
Actually, the first one should be the fully qualified name of StyleCop analyser class and could be found from the source code (e.g. from here).
The second one should start with rule code, then colon and the name of the rule enumeration (luckily, it always looks like the rule name displayed in the Settings Editor, but with no whitespaces).
2.
Regarding suppressing rules "globally" - why don't just turn them off via Settings Editor? Settings files are inherited through the file system, so you could easily have one "main" settings file at the "top" of your folder structure, and some other files (holding the "difference" from main) with exceptions made for some projects, if you want so (like described here).
Good luck!
You can disable the rules you don't want in Settings.StyleCop file, which is in the project root folder.
You will need the namespace that contains the rule, which can be found here:
http://stylecop.soyuz5.com/StyleCop%20Rules.html
Settings.stylecop file code for your reference:
<StyleCopSettings Version="105">
<Analyzers>
<Analyzer AnalyzerId="StyleCop.CSharp.LayoutRules">
<Rules>
<Rule Name="ElementsMustBeSeparatedByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
<AnalyzerSettings />
</Analyzer>
</Analyzers>
</StyleCopSettings>
Alternatively you could move the code in regions into partial classes. Then the issue with the stylecop rule will go away.
In addition to the helpful answers already in place:
If you suppress a warning in the suppression file GlobalSuppressions.cs,
you can edit that [assembly: SuppressMessage(StyleCop...blabla line and entirely remove the Scope=... and Target=... tags. That makes the suppression global in the project.
The README.md for the StyleCop.Analyzers NuGet package used by Visual Studio 2015+ contains a link to the documentation for the rules. The documentation for each rule contains a "How to suppress violations" section. For the SA1202 rule, the options are:
[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "Reviewed.")]
and
#pragma warning disable SA1202 // ElementsMustBeOrderedByAccess
#pragma warning restore SA1202 // ElementsMustBeOrderedByAccess