How to custom rule CA1502 in visual studio project 2013 - c#

I have a problem about custom rule for my project. My case used Microsoft Extended Design Guidline Rules and checked the rule CA1502 in Microsoft.Maintainability. Because the warning message just show when my method has cyclomatic complexity great than 25, but I want to show warning message when it great than 15. Could you make a step by step to config the problem?.
Let me know how to run MSBuild fxcop file. Please give me the script to run.
Thanks everybody,

Related

How to show all Suggestions/Warnings/Errors in the Error List windows of Visual Studio 2019

The problem is that not all visual studio suggestions, warnings and/or errors are shown on solution build.
Let's say that on solution build, a total of 22 warnings are shown. After solution build, upon opening .cs files within projects, more warnings are being shown.
Steps:
Rebuild solution
Error List shows 0 Errors, 22 Warnings, 0 Suggestions
Open a .cs file from within a .csproj
Error List shows 0 Errors, 22+ Warnings, 0+ Suggestions
Why aren't these errors/warnings/suggestions shown on build, rather than being shown once a corresponding .cs file is opened?
Note - An .editorconfig file is used within the solution. This .editorconfig specifies a set of rules which define when to show Errors, Warnings, and Suggestions within .cs files.
I faced this issue and, for me, what worked was simply following the below steps:
Analyze -> Run code Analysis -> On Solution
This will give all warnings on solution level.
In Visual Studio 2022 I missed many warnings too - via Tools > Options, tab Text Editor > C# > Advanced you find these settings:
Run background code analysis for:
Show compiler errors and warnings for:
and for both you probably want the option Entire solution (instead of Open documents, Current document or None).
You can enable Source code analysis for a single project through the Project Properties dialog. This will in fact install some NuGet packages into the project, which together will run an analysis on each build of the project.
This is not an exact replica of Intellisense analysis, if only because what it will do depends on the selected Rule Set. For the most part I expect this to be at least as thorough as the Intellisense analysis, and probably even more so.
Here is how to install it, and how to select a Rule Set:
For more info, see
https://learn.microsoft.com/en-us/visualstudio/code-quality/roslyn-analyzers-overview
https://azuredevopslabs.com/labs/devopsserver/codeanalysis/

Hierarchy of code analysis rulesets?

I've got a solution containing two MVC 5 web applications with associated class libraries and the code analysis settings are causing the build to hang. If I try to interact with the UI during this time I get the "VS is busy" bubble. Leaving the build to complete overnight doesn't work either.
To troubleshoot this I turned off code analysis on all projects and the project builds just fine [0]. So I enabled the "Microsoft All Rules" on one of the MVC projects and the build process doesn't complete.
"Microsoft Managed Minimum Rules" builds but what I'd now like is that there's some kind of structured way of going through the rulesets, where the next one I try is a superset of the last successful one. Does such a hierarchy exist, and if so, is there a canonical reference for it?
Once I get to that level then I can start to isolate individual rules, perhaps by increasing the verbosity of the build output...
[0]
This statement should not be interpreted as "Building without code analysis is perfectly okay"
A general hierarchy is exposed via the Include elements in the .ruleset files located under the Visual Studio install directory (e.g.: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets for a typical VS 2015 installation). Broadly, it looks something like this (with "All Rules" not actually depending on any of the others):
All Rules
Extended Correctness Rules
Basic Correctness Rules
Minimum Recommended Rules
Extended Design Guideline Rules
Basic Design Guideline Rules
Minimum Recommended Rules (same as above)
Globalization Rules
Security Rules
It's also worth noting that this isn't a clean hierarchy without overlaps. For example, rules included in the "Globalization" and "Security" rulesets are also included in some of the others (including the "Minimum" set).
To inherit from a ruleset file, you can include it with:
<Include Path="MyOther.ruleset" Action="Default" />
Then you can override the action for specific rules.

FxCop Rule in SonarQube 4.5.7 on C# project

I'm tring to run analysis on SonarQube, using an FxCop custom Rule.
In SonarQube 4.5.7 I add the rule to the set of rules, I activate it and then run the analysis.
To run the analysis I use the sequence of following commands:
1) MSBuild.SonarQube.Runner.exe begin /k:my.project.C-Sharp-ConsoleApp /n:C-Sharp-ConsoleApp /v:1.1
2) "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild" /T:Rebuild
3) MSBuild.SonarQube.Runner.exe end
I see that the rule is executed, because when I run the second command I read the following part of log:
...
(RunCodeAnalysis target) ->
MSBUILD : warning CR1000: MyRules : Field 'CSharpSortNames.m_variabile' is not in Hungarian notation. Field name should be prefixed with 's'. [C:\Users\Alessandro\Documents\Visual Studio 2015\Projects\C-Sharp-ConsoleApp\C-Sharp-ConsoleApp\C-Sharp-ConsoleApp.csproj]
My custom rule checkID is CR1000, and after the third command, I see that an error of this rule is founded, but the web app doesn't let me see where. For all other errors the web app let me see the precise line where is the error by a link to the .cs file. For my rule it doesn't.
Anyone can help me about this?
Further problem is in SonarQube 5.4 the same rule is activated but web app does not show the error.
The root cause here is that FxCop uses information from the PDB file for providing location information. However, the PDB only contains information that would be useful for debugging scenarios, which means that FxCop rule violations that are associated with non-executable code (e.g.: field declarations or interface definitions) will not have location information available. (FWIW, there is an open SonarQube issue for addressing this, but it would be non-trivial to accomplish unless SonarQube were to directly examine the source files to attempt to locate the field declaration. I rather suspect they might not bother given that it simpler to address via a Roslyn analyzer.)
Further problem is in SonarQube 5.4 the same rule is activated but web
app does not show the error.
That's because older versions of the C# plugin for SonarQube completely ignored FxCop violations without location information. This was addressed in version 5.2 of the plugin, which only became in early May 2016 (and is presumably what you used when you installed SonarQube 5.5). It is compatible with version 5.4 of SonarQube, so you should be able to use it with your older installs if you like.

Inetgrating custom Fxcop rule with existing Fxcop rule

I have developed some Fxcop custom rule and I want to create seperate project just like 'Design Rules','Globalization Rules' etc which will contain custom rule which was designed by me and some selected existing Miscrosoft Fxcop Rules. I want to know whether it is possible to do so. If possible ,How ?
You would have to place the custom rule dll-file in your correct FxCop-rulelocation:
This can be C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static Analysis Tools\FxCop\Rules for example. Find your location and place it in there.
What you want to do next is create a Code Analysis Ruleset.
There is a Microsoft tutorial on creating Code Analysis Rulesets here.
To open an empty rule set file in the rule set editor
On the File menu of Visual Studio, point to New and then click File.
In the New File dialog box, click General in the Installed Templates list, and then select Code Analysis Rule Set.
The rule set editor appears. No rules are selected in the editor list.
What you see next is the Rule Set Editor. Here you can select existing rules and also your own, custommade rules.
Working with the ruleset editor is explained here.
There is a step-by-step guide available at http://blogs.msdn.com/b/codeanalysis/archive/2010/03/26/how-to-write-custom-static-code-analysis-rules-and-integrate-them-into-visual-studio-2010.aspx for authoring rules and adding them to .ruleset files. To figure out where the problem is originating, it would be best to follow all the recommended steps, including verifying that the rule can be executed by the fxcopcmd.exe command line tool.

Disable stylecop analysis for specific projects within solution

Is there a way to easily stop StyleCop warnings from being displayed within specific projects in a solution. Or, more pointedly, a way to select which projects StyleCop analysis should be performed on by Visual Studio?
We have a solution with 9 projects in it. There are only 2 projects that I actually want to see StyleCop warnings for, so I've created StyleCop.Settings files within those project root directories. This means that, for the rest of the projects, the default rule set is applied and I get a screen full of warnings every time I open a class.
The only way I can think to remove these warnings is to add another StyleCop.Settings file a folder level above with all the rules switched off and set merge options on the specific Settings files I am interested in to not merge with this parent file. This just feels wrong though. Is there a cleaner option or is this my only one?
UPDATE: I'm specifically looking for a way to stop the warnings from appearing within Visual Studio. I've added a Settings.StyleCop file to the solution folder and disabled all the rules. I run StyleCop analysis across one of my test projects and there are no errors reported. However, opening a test class reveals a raft of StyleCop warnings, which I want to suppress. Could this be the StyleCop for ReSharper plugin? I have a code cleanup profile created and have disabled certain rules within there but that doesn't appear to make any difference within my test classes.
Please have a look at File Lists configuration - they allow to disable rules by default per project:
Enabled Or Disabled By Default
In addition, a new setting allows you to determine whether rules
should be enabled or disabled by default. This can be set either at
the project level or at the SourceFileList level. For example, here’s
how you would set up a project with all rules disabled by default, and
only two rules explicitly enabled:
<StyleCopSettings Version="4.3">
<GlobalSettings>
<BooleanProperty Name="RulesEnabledByDefault">False</BooleanProperty>
</GlobalSettings>

Categories

Resources