AgentSkillsCN

deadcode

在代码库中查找并移除已废弃、未使用或无法访问的代码。当用户输入“/deadcode”,或希望查找未使用的代码、废弃代码、无法访问的代码、孤立文件、未使用的导入、未使用的变量,或清理代码库时,可使用此功能。触发条件:dead code、unused、unreachable、orphan、cleanup、prune。

SKILL.md
--- frontmatter
name: deadcode
description: "Find and remove dead, unused, or unreachable code in a codebase. Use when the user says /deadcode, asks to find unused code, dead code, unreachable code, orphaned files, unused imports, unused variables, or wants to clean up a codebase. Triggers: dead code, unused, unreachable, orphan, cleanup, prune."

Dead Code Detector

Find and safely remove dead, unused, or unreachable code.

Workflow

  1. Determine scope:
    • Specific file/directory or entire project.
    • Specific language(s) to focus on.
  2. Identify the project type and entry points:
    • Check for package.json, pyproject.toml, Cargo.toml, go.mod, etc.
    • Identify entry points (main files, exported modules, route handlers).
  3. Scan for dead code by category:

Categories to Check

Unused Imports/Requires

  • Scan each file for imported symbols not referenced in the file body.

Unused Exports

  • Find exported functions/classes/constants and search the codebase for their usage.
  • Use grep -r or equivalent to verify each export is imported elsewhere.

Unused Variables & Parameters

  • Identify declared variables never read.
  • Find function parameters never referenced in the function body.
  • Respect _ prefix convention for intentionally unused params.

Unreachable Code

  • Code after unconditional return, throw, break, continue.
  • Conditions that are always true/false (e.g., if (false)).

Unused Files

  • Files not imported/required by any other file.
  • Test files and config files should be excluded from this check.

Commented-Out Code

  • Large blocks of commented code (>5 lines) that are likely stale.
  1. Present findings grouped by category with file paths and line numbers.
  2. For each finding, assess confidence (high/medium/low) based on static analysis limitations.
  3. Remove only with user approval, starting with high-confidence items.

Guidelines

  • Never auto-delete — always present findings and ask for confirmation.
  • Respect dynamic usage patterns: getattr(), eval(), reflection, decorators, dependency injection.
  • Check for framework conventions (e.g., Django views referenced in urls.py, React components used in JSX).
  • Exclude test files, config files, and type definition files from "unused" reports.
  • For libraries/packages, exported public API should not be flagged as dead code.
  • Note limitations: static analysis cannot catch all dynamic references.