Code Analysis Rule Sets in Visual Studio 2010
- by Anthony Trudeau
Microsoft Visual Studio 2010 introduces the concept of rule sets when configuring code analysis. This is a valuable change from Visual Studio 2008 that I didn't even realize I wanted. Visual Studio 2008 by default selected all rules and then you had to remove rules on an item by item basis.
The rule sets fall into logical groups including "Microsoft All Rules", "Microsoft Basic Correctness Rules", "Microsoft Security Rules", et al. And within the project properties you can select one rule set, multiple rule sets, or you can define your own rule set based upon another.
Selecting a single rule set is obviously the easiest option. The default rule set when you create a new project is the "Microsoft Minimum Recommended Rules". However, in my opinion the recommended rules are just too permissive. For that reason you might want to change your rule set to "Microsoft All Rules" until you get around to creating your own rule set; or alternately you can select multiple rule sets which is an option from the rule set combo box. The Visual Studio documentation has comprehensive help on what is contained within the rule sets.
Creating your own rule set is easy if not obvious. You need to start a rule set from an existing rule set. To get started select a rule set in the combo box within the Code Analysis tab of the project properties. I selected the "Microsoft All Rules" for my rule set, but you may find it easier to start with the "Microsoft Minimum Recommended Rules" if your rules are on the more permissive side.
Once your rule set is selected click the Open button. This will display a dialog that is similar in composition to the rules selection from Visual Studio 2008. Browsing through the tree view you can select or deselect individual rules within their categories; and you can indicate that the rules are flagged as errors instead of the default which is a warning. A nice touch to the form is that you get a help pane when you select an individual rule. That helped me considerably when I first configured my rule set.
Once you have finished selecting your rules click the Save tool button, specify a location and name, and click the Save button on the Save As dialog. Once you're back on the Code Analysis tab you'll choose the Browse option within the combo box and open the file you just created.