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 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 Core 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.
-
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
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 repository.