Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/menees/Analyzers

C# code analyzers
https://github.com/menees/Analyzers

analyzer c-sharp roslyn roslyn-analyzer

Last synced: 1 day ago
JSON representation

C# code analyzers

Awesome Lists containing this project

README

        

![windows build & test](https://github.com/menees/Analyzers/workflows/windows%20build%20&%20test/badge.svg) [![Nuget](https://img.shields.io/nuget/v/Menees.Analyzers)](https://www.nuget.org/packages/Menees.Analyzers/)

# 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](https://github.com/DotNetAnalyzers/StyleCopAnalyzers) and [Code-Cracker](https://github.com/code-cracker/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](src/Menees.Analyzers). 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](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1027.md) rule. This is similar to the StyleCop+ rule [SP2001: CheckAllowedIndentationCharacters](https://stylecopplus.codeplex.com/#MoreCustomRules) 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](https://stylecopplus.codeplex.com/#MoreCustomRules). 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](https://stylecopplus.codeplex.com/#MoreCustomRules). 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](https://stylecopplus.codeplex.com/#MoreCustomRules). 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](https://stylecopplus.codeplex.com/#MoreCustomRules). 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](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1124.md) 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](https://github.com/menees/VsTools) 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](http://blogs.msdn.com/b/brada/archive/2004/07/29/201354.aspx)). This rule is a complement to [CA2201](https://msdn.microsoft.com/library/ms182338.aspx), 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](https://en.wikipedia.org/wiki/Magic_number_(programming)#Accepted_limited_use_of_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](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1200.md)) 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](https://msdn.microsoft.com/en-us/library/ms182335.aspx). |
| 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](https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1726?view=vs-2019#rule-description) except this rule only checks single terms (not double terms). So it uses a slightly different set of [default preferred terms](https://github.com/menees/Analyzers/blob/172bf0b6820e143de28f2f43e712908179ca1073/src/Menees.Analyzers/Settings.cs#L53-L77) (omitting double terms like LogOn), and it includes Id as a preferred term over ID (per FxCop's [CA1709 rule](https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1709?view=vs-2019#rule-description)). |
| MEN016 | Avoid Top-Level Statements | C# [top-level statements](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements) are only for toy/example programs and should be avoided in long-term code for [consistency and maintainability](https://github.com/dotnet/docs/issues/27420#issuecomment-988776134). |
| MEN017 | Remove Unused Private Setter | A private set accessor is not needed when an auto property is only assigned in the constructor. Inspired by [C# Essentials' Use Getter-Only Auto-Property](https://github.com/DustinCampbell/CSharpEssentials?tab=readme-ov-file#use-getter-only-auto-property). |
| MEN018 | Use Digit Separators | Numeric literals should use digit separators ('_' from C# 7) to improve readability. This applies to hexadecimal, binary, and [integer](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types#integer-literals) or [real](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types#real-literals) literals. |