SuperAPI AI 工具集

swift_deep_insight

编程与开发

查看项目

更新于 10-16 06:26

技能介绍

Swift Deep Insight:未使用代码检测

Swift Deep Insight 用于检测 Swift 项目中的未使用(死)代码,分析依据是 Swift 编译器的索引存储(index store)。它可以检测未使用的类、结构体、函数、属性、协议等声明。

前置条件

Xcode 项目

先在 Xcode 中构建项目——索引存储会自动创建在 ~/Library/Developer/Xcode/DerivedData/<Project>/Index/DataStore。

SPM 项目

使用 xcodebuild -scheme <scheme> build 或 swift build 进行构建。

命令

从项目根目录运行,或指定路径:

扫描当前目录

swift run swift-deep-insight --project-path .

详细输出

swift run swift-deep-insight --project-path . --verbose

Xcode 可点击输出(file:line:column:)

swift run swift-deep-insight --project-path . --xcode-format

包含 public 声明(通常被排除)

swift run swift-deep-insight --project-path . --include-public

选项

选项 说明
--project-path Swift 项目路径(默认:当前目录)
--verbose 启用详细日志
--xcode-format 以 Xcode 兼容格式输出(file:line:column:)
--include-public 在未使用分析中包含 public 声明
--exclude-tests 从分析中排除测试目标
--build 扫描前先构建项目(默认:false)

注意事项

  • 没有索引存储 = 结果有限:没有索引存储时,分析会退回到仅解析源码,从而遗漏跨文件引用。务必先构建。
  • 运行时反射不可见:通过 responds(to:)、perform() 或其他运行时机制使用的代码,即使实际被调用,也会显示为未使用。
  • 不分析外部包:只扫描你自己项目的代码,不扫描依赖项。
  • 默认排除 public 声明:public/API 符号被假定由使用方使用。可用 --include-public 覆盖。
  • 入口点自动检测:main、@main、@NSApplicationMain 和 AppDelegate 被视为根——从它们可达的一切都算“已使用”。

工作流程

  1. 构建:确保项目已构建,使索引存储是最新的
  2. 扫描:使用合适的标志运行分析器
  3. 审查:检查未使用的声明——在删除前确认每个都确实是死代码
  4. 验证:删除后重新构建并运行测试,确认没有内容被错误分类