dead-code

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GitNexus Dead Code Detector

GitNexus 死代码检测器

CLI ONLY — no MCP server exists. Never use
readMcpResource
with
gitnexus://
URIs.
Find orphan functions, dangling imports, isolated files, and unreachable code using the GitNexus knowledge graph.
仅支持CLI — 不存在MCP服务器。切勿对
gitnexus://
URI使用
readMcpResource
借助GitNexus知识图谱查找孤立函数、悬垂导入、隔离文件和不可达代码。

When to Use

适用场景

  • Periodic codebase cleanup
  • Before a major release to reduce surface area
  • After a refactor to find newly orphaned code
  • "Are there any unused functions?"
For language-specific dead code tools (vulture, knip, clippy): use
quality-tools:dead-code-detector
instead. This skill uses the GitNexus graph for cross-file structural analysis.
  • 定期代码库清理
  • 重大版本发布前缩减代码暴露面
  • 重构后查找新产生的孤立代码
  • 排查「是否存在未使用的函数?」类问题
如需使用特定语言的死代码工具(vulture、knip、clippy):请改用
quality-tools:dead-code-detector
。本功能使用GitNexus图谱实现跨文件结构分析。

Workflow

使用流程

Step 1: Verify Index

步骤1:验证索引状态

Run from the repo root (the CLI auto-detects the repo from cwd):
bash
npx gitnexus@latest status
If stale, suggest running
/gitnexus-tools:reindex
first.
在代码仓库根目录运行(CLI会自动从当前工作目录识别仓库):
bash
npx gitnexus@latest status
如果索引过期,建议先运行
/gitnexus-tools:reindex

Step 2: Run Cypher Queries

步骤2:运行Cypher查询

Run these 4 queries to detect different categories of dead code:
运行以下4个查询检测不同类别的死代码:

Orphan Functions

孤立函数

Functions with no incoming CALLS edges and not participating in any process:
bash
npx gitnexus@latest cypher "MATCH (f:Function) WHERE NOT EXISTS { MATCH ()-[:CALLS]->(f) } AND NOT EXISTS { MATCH ()-[:STEP_IN_PROCESS]->(f) } RETURN f.name, f.file, f.line ORDER BY f.file LIMIT 50"
无入站CALLS边、且不属于任何流程的函数:
bash
npx gitnexus@latest cypher "MATCH (f:Function) WHERE NOT EXISTS { MATCH ()-[:CALLS]->(f) } AND NOT EXISTS { MATCH ()-[:STEP_IN_PROCESS]->(f) } RETURN f.name, f.file, f.line ORDER BY f.file LIMIT 50"

Dangling Imports

悬垂导入

Import edges with low confidence (< 0.5), indicating potentially broken references:
bash
npx gitnexus@latest cypher "MATCH (a)-[r:IMPORTS]->(b) WHERE r.confidence < 0.5 RETURN a.name, b.name, r.confidence, a.file ORDER BY r.confidence LIMIT 30"
置信度低于0.5的导入边,代表可能存在损坏的引用:
bash
npx gitnexus@latest cypher "MATCH (a)-[r:IMPORTS]->(b) WHERE r.confidence < 0.5 RETURN a.name, b.name, r.confidence, a.file ORDER BY r.confidence LIMIT 30"

Dead Code (Unreachable)

死代码(不可达)

Functions unreachable from any entry point — no callers and no process membership:
bash
npx gitnexus@latest cypher "MATCH (f:Function) WHERE NOT EXISTS { MATCH ()-[:CALLS]->(f) } AND NOT EXISTS { MATCH ()-[:STEP_IN_PROCESS]->(f) } AND NOT f.name STARTS WITH '_' AND NOT f.name STARTS WITH 'test_' RETURN f.name, f.file, f.line ORDER BY f.file LIMIT 50"
从任何入口点都不可达的函数:无调用方、且不属于任何流程:
bash
npx gitnexus@latest cypher "MATCH (f:Function) WHERE NOT EXISTS { MATCH ()-[:CALLS]->(f) } AND NOT EXISTS { MATCH ()-[:STEP_IN_PROCESS]->(f) } AND NOT f.name STARTS WITH '_' AND NOT f.name STARTS WITH 'test_' RETURN f.name, f.file, f.line ORDER BY f.file LIMIT 50"

Isolated Files

隔离文件

Files with no imports in or out:
bash
npx gitnexus@latest cypher "MATCH (f:File) WHERE NOT EXISTS { MATCH (f)-[:IMPORTS]->() } AND NOT EXISTS { MATCH ()-[:IMPORTS]->(f) } RETURN f.path ORDER BY f.path LIMIT 30"
无入站或出站导入的文件:
bash
npx gitnexus@latest cypher "MATCH (f:File) WHERE NOT EXISTS { MATCH (f)-[:IMPORTS]->() } AND NOT EXISTS { MATCH ()-[:IMPORTS]->(f) } RETURN f.path ORDER BY f.path LIMIT 30"

Step 3: Filter Results

步骤3:过滤结果

Exclude false positives:
  • Test files (
    **/test_*
    ,
    **/tests/*
    ,
    **/*_test.*
    ) — test functions are legitimately "uncalled"
  • __init__
    files
    — may be legitimately empty or used for re-exports
  • Entry points (
    main
    ,
    cli
    ,
    __main__
    ) — called by the runtime, not by other code
  • Private helpers prefixed with
    _
    — may be used via dynamic dispatch
排除误报场景:
  • 测试文件
    **/test_*
    **/tests/*
    **/*_test.*
    )—— 测试函数本身确实「无调用方」
  • __init__
    文件
    —— 可能本身为空,或用于再导出
  • 入口点
    main
    cli
    __main__
    )—— 由运行时调用,而非其他代码调用
  • 带下划线
    _
    前缀的私有辅助函数
    —— 可能通过动态分发调用

Step 4: Structured Report

步骤4:结构化报告

Present categorized by module/directory:
undefined
按模块/目录分类展示结果:
undefined

Dead Code Report

死代码报告

Orphan Functions (N found)

孤立函数(共找到N个)

FunctionFileLine
.........
函数名文件路径行号
.........

Dangling Imports (N found)

悬垂导入(共找到N个)

SourceTargetConfidence
.........
源文件目标对象置信度
.........

Isolated Files (N found)

隔离文件(共找到N个)

  • path/to/file.py
  • ...
  • path/to/file.py
  • ...

Summary

汇总

  • Total orphans: N
  • Total dangling: N
  • Total isolated: N
undefined
  • 孤立函数总数:N
  • 悬垂导入总数:N
  • 隔离文件总数:N
undefined

Notes

注意事项

  • Results depend on index freshness — reindex if results seem wrong
  • The graph captures static relationships only — dynamic dispatch, reflection, and plugin loading may cause false positives
  • Use
    quality-tools:dead-code-detector
    for language-specific analysis (vulture, knip, clippy) which complements this structural view
  • 结果取决于索引新鲜度 —— 如果结果看起来有误请重新索引
  • 图谱仅捕获静态关系 —— 动态分发、反射和插件加载可能导致误报
  • 可使用
    quality-tools:dead-code-detector
    进行特定语言分析(vulture、knip、clippy),可与本结构分析能力互补