custom threshold for CA1502 in visual studio 2013 ultimate - visual-studio-2013

This question: Custom threshold for CA1502
discusses how to set up custom thresholds for code metrics rules in code analysis.
I have the same problem, but think that the old question is out of date.
To repeat:
In particular, we would like our Build to fail when a method has a
code complexity of more than 20. Unfortunately, rule CA1502 has a
threshold of 25:
The rule reports a violation when the cyclomatic complexity is more
than 25.
Can we somehow change this?
The accepted answer is to edit an .fxcop file to include the rules.
In visual studio 2013 ultimate we have integrated Code Analysis and Code metrics; but we don't appear to have .fxcop rules - I think this used to be when fxcop was a separate extension.
Is there any way to edit the thresholds in the .ruleset files produced by visual studio?
Or have I missed how and where to get .fxcop files in the more recent versions?

The post you cited is actually pretty recent. The use of .fxcop files is still the only way to configure the rules. (If you're not fond of this limitation, you might want to vote at http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2406555-allow-code-analysis-rules-to-be-configured-via-ru.)
You can use the FxCop 10.0 UI to create a "seed" .fxcop file, but you will need to edit it by hand to add the settings. To integrate with Visual Studio, you can use the CodeAnalysisProject MSBuild property to specify the relative path to the .fxcop file.

Related

Rule to build solution before check-in in Visual Studio

Is it possible to use StyleCop to enforce these two rules?
Get latest version before check in
Build solution with a
specific configuration before check in
Any other suggestions besides StyleCop are welcomed. Thank You.
No. Stylecop is an open source static code analysis tool from Microsoft that checks C# code for conformance to StyleCop's recommended coding styles and a subset of Microsoft's .NET Framework Design Guidelines.
(wikipedia)
To enforce the getting of the latest version before checking in, you could use a custom TFS policy. This seems to have been successful for other users.
To check that a particular solution builds before checking in, you could use a Gated Check-in for that solution. The code won't be committed if that solution (and it's tests do not pass).

In Visual Studio 2013 how do I limit code analysis to my code and not to referenced assemblies?

I have a solution in Visual Studio 2013 (c# code) that has a reference to a third-party assembly that I added with NuGet (ICSharpCode.SharpZipLib). When I run the "Code Analysis" on the solution I get lots of warnings coming from the third-party assembly. How do I tell VS that I only want code analysis advice from my code?
I'm pretty sure the errors are coming from the library itself and not my usage of it. There are a total of 32 issues e.g.
CA2237: Mark ISerializable types with SerializableAttribute.
I am using ILMerge to bundle into a single executable do you think that has confused the code analysis?
There is no way to tell FxCop to ignore namespaces or types in your code if it lives in the same binaries. When using ILMerge, the SharpZipLib essentially becomes part of your code, as it's merged into your assembly.
To get proper results, run FxCop on the assembly that's generated before the merge.
Other solutions will mention to put a [GeneratedCode] attribute on classes you want excluded, but IlMerge has no option to to that while merging, so that won't help you out.
In Visual Studio 2015 Code Analysis works on the source code using the new Roslyn Engine, which will solve the issue probably, as it does the analysis even before compilation and as such will not be impacted by your use of IlMerge.

What is the difference between code analysis and sonar?

I have a set of rules defined for Visual Studio Code Analysis and I also configured them in Sonar (http://www.sonarqube.org/). I get different results.
Why?
Code Analysis uses FxCop ? In Sonar I can configure FxCop.
Well, the reason is simple: these are different tools, with different features and capabilities...
In SonarQube, you can configure the quality profile (= rule set) that you want to apply on your project when you analyse it. This rule set is probably very different from the one available in Visual Studio Code Analysis.

How to get FxCop have the same set of rules as that of Visual Studio Code Analysis?

Yesterday I posted a question here ( FxCop and Code Analysis Rules ) about getting FxCop to run the same rules as Code Analysis and I thought it was resolved.
However it seems like there's no real way to get FxCop to run the same exact set of rules as Visual Studio 2010's Code Analysis. For instance once I got everything working with my FxCopCmd setup it started barking about rules denoted in the Minimum Recommended Rules ruleset like CA2000. If I remove the rules it doesn't like it runs the analysis and everything looks good . . . except it's really not running the same ruleset!
This seems kind of crazy to me! Is there anyway to get FxCop to analyze the exact same set of rules that Visual Studio Code Analysis can analyze against? Is there a .dll to add to the rules to analyze to get the rules like CA2000 and others?
VS Code Analysis includes some rules that are not included with stand-alone FxCop. You will need to ensure that the extra rule assemblies are available to fxcopcmd.exe. The easiest way to do this is to replace the contents of your build-source FxCop folder with those from your Visual Studio installation since one of the rule assemblies (DataflowRules.dll) uses an analysis engine that isn't included with stand-alone FxCop either, and that depends on a bunch of other assemblies that are also included in the FxCop folder under the VS installation folder hierarchy.

Visual Studio class/file templates: Is there a way to change their content automatically per project/solution?

I've updated my default templates in Visual Studio for classes, interfaces, code files, etc. I removed the default namespaces and added a copyright header blurb.
Is there a way to use a variable or something in the template so I don't have to zip/unzip and re-run the vs installer to change the copyright header? (I'm a consultant, the code-owner isn't always me or my company).
Yes, you can. The documentation for this sort of thing is part of the Visual Studio SDK. There are already many variables you can use.
If you find you want to get fancy, look into the Guidance Automation Toolkit. A template using GAT can accept user input as well as information from the project and environment, can unfold one or more templates, filling in placeholders with the data gathered, and then can execute various actions against the unfolded templates, the project, or whatever.
You can get the complete example implemented here: Multi-Project Templates with Wizard: Visual Studio 2010 Sample

Resources