AgentSkillsCN

roslynator

在 .NET 项目中运行 C# 静态分析,自动修复代码问题,格式化代码,查找未使用的代码,并严格执行编码标准。适用于用户询问代码质量、代码 linting、静态分析、代码清理、未使用代码,或 C#/.NET 项目的代码格式化时使用。

SKILL.md
--- frontmatter
name: roslynator
description: Run C# static analysis, auto-fix code issues, format code, find unused code, and enforce coding standards in .NET projects. Use when the user asks about code quality, linting, static analysis, code cleanup, unused code, or formatting in C# / .NET projects.
argument-hint: "[solution or project path]"

Prerequisites

Check if roslynator is installed:

code
which roslynator

If not found, try running via dnx (no install needed, .NET 10+):

code
dnx Roslynator.DotNet.Cli [command] [options]

Or install globally:

code
dotnet tool install -g roslynator.dotnet.cli

About Roslynator

Roslynator is a set of code analysis tools for C# powered by Roslyn. It provides 500+ analyzers, refactorings, and code fixes. The CLI tool can analyze, fix, and format entire solutions from the command line — no IDE needed.

The CLI itself contains no analyzers — they come from NuGet packages referenced in your project (e.g. Roslynator.Analyzers) or via --analyzer-assemblies.

Common Usage

Fix all diagnostics in a solution:

code
roslynator fix MySolution.sln

Fix all diagnostics in a project:

code
roslynator fix MyProject.csproj

Analyze without fixing (report only):

code
roslynator analyze MySolution.sln

Format code:

code
roslynator format MySolution.sln

Find unused code (dead code detection):

code
roslynator find-unused MySolution.sln

List symbols:

code
roslynator list-symbols MySolution.sln

Count lines of code:

code
roslynator lloc MySolution.sln

Commands

CommandPurpose
fixAuto-fix diagnostics in project/solution
analyzeReport diagnostics without fixing
formatFormat whitespace
find-unusedFind unused declarations (dead code)
list-symbolsList types, members, and symbols
llocCount logical lines of code
locCount physical lines of code
spellcheckCheck spelling in comments and strings

Key Flags (shared across commands)

FlagPurpose
--projects <name>Only process named projects
--ignored-projects <name>Skip specific projects
--include <glob>Include matching files/folders
--exclude <glob>Exclude matching files/folders
-v, --verbosity <level>Output level: quiet, minimal, normal, detailed, diagnostic
-p, --properties <NAME=VALUE>MSBuild properties (e.g. Configuration=Release)
-m, --msbuild-path <dir>Path to MSBuild directory
--language <lang>Language: cs or vb
-g, --include-generated-codeInclude generated code
--file-log <path>Write output to file
-h, --helpShow help

Analyze-specific Flags

FlagPurpose
-a, --analyzer-assemblies <path>Paths to additional analyzer assemblies
--supported-diagnostics <id>Report only these diagnostic IDs
--ignored-diagnostics <id>Skip these diagnostic IDs
--severity-level <level>Minimum severity: hidden, info, warning, error
--ignore-compiler-diagnosticsHide compiler messages
-o, --output <path>Save diagnostics to file
--output-formatReport format: xml or gitlab
--execution-timeMeasure analyzer performance

Configuration

Roslynator is configured via .editorconfig in your project:

Set severity for all Roslynator analyzers:

ini
[*.cs]
dotnet_analyzer_diagnostic.category-roslynator.severity = warning

Enable/disable specific analyzers:

ini
[*.cs]
dotnet_diagnostic.RCS1001.severity = none        # Disable specific rule
dotnet_diagnostic.RCS1036.severity = error        # Upgrade to error

Enable/disable refactorings:

ini
[*.cs]
roslynator_refactoring.add_braces.enabled = false

Exit Codes

CodeMeaning
0Success — no diagnostics found or all fixed
1Diagnostics found or not all fixed
2Error or execution canceled

Workflow with pre-pr

Roslynator is used in the /pre-pr quality gate as the first step:

  1. roslynator fix — auto-fix code issues
  2. dotnet build — verify compilation
  3. dotnet test — run tests
  4. quickdup — check for duplication
  5. dotnet format — format code

Guidelines

  • Always build the solution before running roslynator analyze — it needs compiled output
  • Use roslynator fix for auto-fixing, roslynator analyze for CI reporting
  • Use --severity-level warning to skip info/hidden diagnostics in CI
  • Use --ignored-diagnostics to suppress known false positives
  • Use roslynator find-unused periodically to catch dead code
  • Configure rules in .editorconfig rather than via CLI flags for consistency across team
  • When the user mentions "lint", "static analysis", or "code quality" in a .NET context, this is the tool to use