dead-code
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGitNexus Dead Code Detector
GitNexus 死代码检测器
CLI ONLY — no MCP server exists. Never usewithreadMcpResourceURIs.gitnexus://
Find orphan functions, dangling imports, isolated files, and unreachable code using the GitNexus knowledge graph.
仅支持CLI — 不存在MCP服务器。切勿对URI使用gitnexus://。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 instead. This skill uses the GitNexus graph for cross-file structural analysis.
quality-tools:dead-code-detector- 定期代码库清理
- 重大版本发布前缩减代码暴露面
- 重构后查找新产生的孤立代码
- 排查「是否存在未使用的函数?」类问题
如需使用特定语言的死代码工具(vulture、knip、clippy):请改用。本功能使用GitNexus图谱实现跨文件结构分析。
quality-tools:dead-code-detectorWorkflow
使用流程
Step 1: Verify Index
步骤1:验证索引状态
Run from the repo root (the CLI auto-detects the repo from cwd):
bash
npx gitnexus@latest statusIf stale, suggest running first.
/gitnexus-tools:reindex在代码仓库根目录运行(CLI会自动从当前工作目录识别仓库):
bash
npx gitnexus@latest status如果索引过期,建议先运行。
/gitnexus-tools:reindexStep 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 functions are legitimately "uncalled"**/*_test.* - files — may be legitimately empty or used for re-exports
__init__ - Entry points (,
main,cli) — called by the runtime, not by other code__main__ - 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按模块/目录分类展示结果:
undefinedDead Code Report
死代码报告
Orphan Functions (N found)
孤立函数(共找到N个)
| Function | File | Line |
|---|---|---|
| ... | ... | ... |
| 函数名 | 文件路径 | 行号 |
|---|---|---|
| ... | ... | ... |
Dangling Imports (N found)
悬垂导入(共找到N个)
| Source | Target | Confidence |
|---|---|---|
| ... | ... | ... |
| 源文件 | 目标对象 | 置信度 |
|---|---|---|
| ... | ... | ... |
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
undefinedNotes
注意事项
- 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 for language-specific analysis (vulture, knip, clippy) which complements this structural view
quality-tools:dead-code-detector
- 结果取决于索引新鲜度 —— 如果结果看起来有误请重新索引
- 图谱仅捕获静态关系 —— 动态分发、反射和插件加载可能导致误报
- 可使用进行特定语言分析(vulture、knip、clippy),可与本结构分析能力互补
quality-tools:dead-code-detector