StyleChecker is yet another code style checker and refactoring tool like FxCopAnalyzers, StyleCop Analyzers, SonarLint, Roslynator, and so on. It uses the .NET Compiler Platform ("Roslyn") to analyze the C# source code of .NET Core projects and outputs diagnostics of a rule violation, and when running with Visual Studio it provides code fixes as much as possible. Note that StyleChecker contains only supplemental or niche analyzers, so it is intended to be used together with other Roslyn analyzers.

Get started

StyleChecker is available as the NuGet-logo NuGet package.

Install StyleChecker to your project with Visual Studio

  1. Open Package Manager Console. (Open your project with Visual Studio, and select Tools ➜ NuGet Package Manager ➜ Package Manager Console.)
  2. Enter the command Install-Package StyleChecker in the Package Manager Console.

Install StyleChecker to your project with .NET Core CLI

  1. Enter the command dotnet add package StyleChecker with the console.

List of diagnostics

There are the following categories of the diagnostics analyzers: Cleaning, Document, Naming, Ordering, Refactoring, Settings, Size, and Spacing.

Category: Cleaning

ByteOrderMark

Remove a UTF-8 BOM.

RedundantTypedArrayCreation

Use an implicitly-typed array creation.

UnusedUsing

Remove unnecessary using directives.

UnusedVariable

Remove unused local variables.

Category: Document

NoDocumentation

A replacement for CS1591 (Missing XML comment for publicly visible type or member), SA1600 (Elements should be documented), and so on. It can be configured so that the entity with the specified attribute can be ignored.

StrayText

Fix the misplaced text in the Documentation Comments.

Category: Naming

SingleTypeParameter

Use T as a type parameter name if the type parameter is single.

ThoughtlessName

Avoid thoughtless names for the identifier of local variables.

Underscore

Avoid including an underscore character (_) in the identifier of local variables.

Category: Ordering

PostIncrement

Avoid post-increment/decrement operators (e.g. i++) if they can be replaced with pre-increment/decrement ones.

Category: Refactoring

AssignmentToParameter

Avoid assignment to the parameters passed by value.

DiscardingReturnValue

Don't discard the return value of some delicate methods like System.IO.Stream.Read(byte[], int, int).

EmptyArrayCreation

Don't create empty arrays, use System.Array.Empty<T> instead.

EqualsNull

Use is null pattern matching instead of == operator.

IneffectiveReadByte

Avoid invoking ReadByte() method of System.IO.BinaryReader class in incremental for loops.

IsNull

Use == operator with null instead of is null pattern matching.

NotDesignedForExtension

Must design a class for inheritance, or else prohibit it.

NotOneShotInitialization

Declare local variables with one-shot initialization.

StaticGenericClass

Move type parameters from the static class to its methods if possible.

StinkyBooleanExpression

Don't use the conditional operator (?:) where either the second or third operand is a bool literal (true or false), resulting in bool values. It is similar to IDE0057 Conditional expression can be simplified.

TypeClassParameter

Replace the parameter of methods or local functions with a type parameter, if its type is System.Type and every argument for it is a typeof() operator.

UninitializedLocalVariable

Initialize local variables when they are declared.

UnnecessaryUsing

Avoid using statements for some types that have no resources to dispose of.

Category: Settings

InvalidConfig

Validate the configuration file StyleChecker.xml.

Category: Size

LongLine

Avoid a long line. In default, it allows less than 80 columns, but the length can be configured.

Category: Spacing

NoSingleSpaceAfterTripleSlash

Regulate spacing after triple slash (Single Line Documentation Comment). It is a replacement for SA1004.

NoSpaceAfterBrace

Regulate spacing after a brace. This analyzer and the NoSpaceBeforeBrace analyzer are designed to be used together and replace SA1012 and SA1013 with them, allowing us to write empty braces ({}).

NoSpaceAfterSemicolon

Regulate spacing after a semicolon. This analyzer and the SpaceBeforeSemicolon analyzer are designed to be used together and replace SA1002 with them, allowing us to write an infinite for loop with for (;;).

NoSpaceBeforeBrace

Regulate spacing before a brace. This analyzer and the NoSpaceAfterBrace analyzer are designed to use them together and replace SA1012 and SA1013 with them, allowing us to write empty braces ({}).

SpaceBeforeSemicolon

Regulate spacing before a semicolon. This analyzer and the NoSpaceAfterSemicolon analyzer are designed to be used together and replace SA1002 with them, allowing us to write an infinite for loop with for (;;).

Customize configuration

Some analyzers can be customized to change their behaviors, placing the configuration file StyleChecker.xml at the project root. The XML Schema Definition file config.v1.xsd of the configuration file and a sample of the configuration file are provided in the directory StyleChecker/StyleChecker/nuget/samples/ of the source tree (or in ~/.nuget/packages/stylechecker/VERSION/samples/ if you installed StyleChecker with the NuGet package). Note that StyleChecker does not use the XML Schema Definition file, but it helps you edit StyleChecker.xml with the text editor that is able to validate XML documents (for example, Visual Studio IDE, Visual Studio Code, and so on).

Create your own StyleChecker.xml file and place it at your project root, and add the AdditionalFiles element to .csproj file in your project as follows:

<ItemGroup>
  <AdditionalFiles Include="StyleChecker.xml" />
</ItemGroup>

Alternatively, with Visual Studio you can set "C# analyzer additional file" to Build Action property (This property can be changed from Solution Explorer ➜ Right Click on the StyleChecker.xml ➜ Properties ➜ Advanced ➜ Build Action).

Documents

How to contribute

Please send us Pull Requests or Issues from the GitHub icon GitHub repository.