StyleChecker is another code style checking and refactoring tool similar to FxCopAnalyzers, StyleCop Analyzers, SonarLint, Roslynator, etc. It uses the .NET Compiler Platform ("Roslyn") to analyze the C# source code of .NET projects and outputs diagnostics of rule violations. And when used with IDEs such as Visual Studio, it provides as many code fixes as possible. Note that you should use this tool with other Roslyn analyzers, as it contains only complementary or niche analyzers.
Get started
StyleChecker is available as the NuGet
package.
Install StyleChecker to your project with Visual Studio
- Open Package Manager Console. (Open your project with Visual Studio, and select Tools ➜ NuGet Package Manager ➜ Package Manager Console.)
- Enter the command
Install-Package StyleChecker
in the Package Manager Console.
Install StyleChecker to your project with .NET CLI
- 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
-
Remove a UTF-8 BOM.
-
Use an implicitly-typed array creation.
-
Remove unnecessary using directives.
-
Remove unused local variables.
Category: Document
-
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.
-
Fix the misplaced text in the Documentation Comments.
Category: Naming
-
Use
T
as a type parameter name if the type parameter is single.
-
Avoid thoughtless names for the identifier of local variables.
-
Avoid including an underscore character (
_
) in the identifier of local variables.
Category: Ordering
-
Avoid post-increment/decrement operators (e.g.
i++
) if they can be replaced with pre-increment/decrement ones.
Category: Refactoring
-
Avoid assignment to the parameters passed by value.
-
Don't discard the return value of some delicate methods like
System.IO.Stream.Read(byte[], int, int)
.
-
Don't create empty arrays, use
System.Array.Empty<T>
instead.
-
Use
is null
pattern matching instead of==
operator.
-
Avoid invoking
ReadByte()
method ofSystem.IO.BinaryReader
class in incrementalfor
loops.
-
Use
==
operator withnull
instead ofis null
pattern matching.
-
Must design a class for inheritance, or else prohibit it.
-
Declare local variables with one-shot initialization.
-
Use Using Declarations to declare a local variable whenever possible.
-
Do not declare a local variable and then check to see whether it is null.
-
Move type parameters from the static class to its methods if possible.
-
Don't use the conditional operator (
?:
) where either the second or third operand is abool
literal (true
orfalse
), resulting inbool
values. It is similar to IDE0057 Conditional expression can be simplified.
-
Replace the parameter of methods or local functions with a type parameter, if its type is
System.Type
and every argument for it is atypeof()
operator.
-
Initialize local variables when they are declared.
-
Avoid
using
statements for some types that have no resources to dispose of.
Category: Settings
-
Validate the configuration file
StyleChecker.xml
.
Category: Size
-
Avoid a long line. In default, it allows less than 80 columns, but the length can be configured.
Category: Spacing
-
Regulate spacing after triple slash (Single Line Documentation Comment). It is a replacement for SA1004.
-
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 (
{}
).
-
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 withfor (;;)
.
-
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 (
{}
).
-
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 withfor (;;)
.
Customize configuration
You can customize some analyzers to change their behaviors by 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 available 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 can validate XML documents (for example, Visual Studio IDE, Visual Studio
Code, and so on).
Create your own StyleChecker.xml
file, place it at your project root, and add
the AdditionalFiles
element to the .csproj
file in your project as follows:
<ItemGroup>
<AdditionalFiles Include="StyleChecker.xml" />
</ItemGroup>
Alternatively, in Visual Studio, you can set the value "C# analyzer additional
file" to the Build Action property. You can change this property 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 repository.