/Analyzers

C# code analyzers

Primary LanguageC#MIT LicenseMIT

windows build & test Nuget

Analyzers

This project provides several new C# code analysis rules for Visual Studio 2015 and later. These rules can be used standalone, or they can be used with other analyzers like StyleCopAnalyzers and Code-Cracker.

Many of the rule limits can be configured using a Menees.Analyzers.Settings.xml file, which must comply with the Menees.Analyzers.Settings.xsd schema. The schema and Settings.cs file (for "documentation") are available in the Menees.Analyzers source code. A project using Menees.Analyzers with a custom settings file should set the Build Action for its Menees.Analyzers.Settings.xml file to "C# analyzer additional file".

This software is CharityWare. If you use it, I ask that you donate something to the charity of your choice.

ID Title Comment
MEN001 Tabs should be used for indentation Ensures tabs are used for indentation instead of spaces. This is the opposite of StyleCop's SA1027: TabsMustNotBeUsed rule. This is similar to the StyleCop+ rule SP2001: CheckAllowedIndentationCharacters when set to "Tabs only".

This rule is off by default because it conflicts with Visual Studio's default settings, which use spaces instead of tabs for indentation. This rule can be enabled using a custom ruleset file, and it includes a code fix provider.
MEN002 Line is too long Ensures that lines are not longer than 160 characters. This is similar to the StyleCop+ rule SP2100: CodeLineMustNotBeLongerThan. The MaxLineColumns and TabSize values for this rule can be configured in Menees.Analyzers.Settings.xml.
MEN002A Line is long Notifies when lines are longer than 160 characters. This rule is off by default because MEN002 takes precedence and is sufficient for most cases. If you enable this rule, then you should also configure NotifyLineColumns to be less than MaxLineColumns in Menees.Analyzers.Settings.xml.
MEN003 Method is too long Ensures that methods are not longer than 120 lines. This is similar to the StyleCop+ rule SP2101: MethodMustNotContainMoreLinesThan. The MaxMethodLines limit for this rule can be configured in Menees.Analyzers.Settings.xml.
MEN004 Property accessor is too long Ensures that property accessors are not longer than 80 lines. This is similar to the StyleCop+ rule SP2102: PropertyMustNotContainMoreLinesThan. The MaxPropertyAccessorLines limit for this rule can be configured in Menees.Analyzers.Settings.xml.
MEN005 File is too long Ensures that files are not longer than 2000 lines. This is similar to the StyleCop+ rule SP2103: FileMustNotContainMoreLinesThan. The MaxFileLines limit for this rule can be configured in Menees.Analyzers.Settings.xml.
MEN006 #regions should be used Recommends that #regions be used when there are over 100 lines in a file or if there is more than one class, struct, enum, or interface defined in a file.

This rule is off by default because it conflicts with StyleCop's SA1124: DoNotUseRegions rule. This rule can be enabled using a custom ruleset file. The MaxUnregionedLines limit for this rule can be configured in Menees.Analyzers.Settings.xml.

Note: Menees VS Tools can be used to easily add, collapse, and expand #regions in C# code (as well as in VB, XML, XAML, HTML, SQL, JavaScript, and TypeScript code).
MEN007 Use a single return Recommends that only a single return statement should be used in a code block. "One entry, one exit" keeps control flow simple and makes refactoring easier.
MEN008 File name should match type Ensures that a file name matches or includes the name of the main type it contains. The TypeFileNameExclusions for this rule can be configured in Menees.Analyzers.Settings.xml.
MEN009 Use the preferred exception type Recommends preferred exception types (e.g., NotSupportedException instead of NotImplementedException). This rule is a complement to CA2201, and it includes a code fix provider.
MEN010 Avoid magic numbers Recommends that named constants be used instead of numeric literals (i.e., magic numbers). The AllowedNumericLiterals for this rule can be configured in Menees.Analyzers.Settings.xml.
MEN011 Align using directives Ensures that using directives are aligned. This is important when using directives are nested inside a namespace (per SA1200: UsingDirectivesMustBePlacedWithinNamespace) because Visual Studio will often fail to indent them correctly if they're added while code contains syntax errors. This rule includes a code fix provider.
MEN012 Flags should be powers of two Flags enum members should be powers of two or bitwise-or combinations of named members. This rule is a complement to CA2217.
MEN013 Use UTC time Recommends UTC times because they're unambiguous and always increasing. This rule includes a code fix provider.
MEN014 Prefer TryGetValue Recommends calling TryGetValue (for a single lookup and retrieval) instead of ContainsKey and this[key] with duplicate lookups.
MEN015 Use Preferred Terms Similar to the old FxCop CA1726 rule except this rule only checks single terms (not double terms). So it uses a slightly different set of default preferred terms (omitting double terms like LogOn), and it includes Id as a preferred term over ID (per FxCop's CA1709 rule).