find-dead-code

bởi openshift

Tìm các hàm, kiểu, hằng số, import không được sử dụng và các đường dẫn mã không thể truy cập. Sử dụng khi người dùng yêu cầu tìm mã chết, mã không dùng, ứng viên dọn dẹp, hoặc…

npx skills add https://github.com/openshift/lightspeed-operator --skill find-dead-code

Find Dead Code

Detect unused Go code that can be safely removed.

Rules

  • Report findings, do not delete. Removal is a separate task.
  • Focus on production code (internal/, api/, cmd/). Skip tests unless explicitly asked.
  • Tools have false positives — classify each finding before recommending removal.
  • Code used via reflection or dynamic dispatch (e.g. Kubernetes controller-runtime, interface implementations) is not dead.

Step 1: Determine Scope

Ask the user:

  • Branch mode: only files changed in the current branch vs main.
  • Full mode: scan the entire codebase.

For branch mode:

git diff --name-only upstream/main -- 'internal/' 'api/' 'cmd/' | grep '\.go$' | grep -v '_test\.go$'

Step 2: Run Go Standard Tooling

Check for unused code with the compiler:

go build ./... 2>&1 | grep "declared and not used"
go vet ./... 2>&1 | grep -E "(not used|never used)"

Step 3: Run staticcheck

staticcheck detects unused code, including unexported functions, constants, and variables:

staticcheck -checks=U1000 ./...

U1000 reports unused code that is not exported and not referenced.

Step 4: Run deadcode (Go 1.23+)

If Go 1.23+ is available:

go run golang.org/x/tools/cmd/deadcode@latest -filter <package-pattern>

This finds functions, types, and variables that are never called or referenced.

Step 5: Check Unused Imports

goimports -l <target>

Any file listed has unused imports. Review with:

goimports -d <file>

Step 6: Filter False Positives

Common false positives in this codebase:

PatternWhy it's not dead
Reconcile(ctx, req) implementationsCalled by controller-runtime via interface
SetupWithManager() functionsCalled by manager setup code
init() functionsCalled automatically by Go runtime
Interface method implementationsCalled through interface, not directly
kubebuilder: marker functionsUsed by code generation
Constants/vars in constants.goMay be used in tests or future code
Error constants matching Err* patternMay be used in error wrapping

Step 7: Classify Findings

For each finding, classify:

CategoryCriteriaAction
RemoveClearly unused, no interface/reflection useSafe to delete
VerifyPossibly used dynamically or via interfaceSearch for references before removing
False positiveInterface impl, reflection, kubebuilder markerSkip

For "Verify" findings, search for references:

rg "<function_or_type_name>" internal/ api/ cmd/ test/

Step 8: Report

For each finding:

  1. File and line number
  2. What is unused (function, type, constant, variable, import)
  3. Tool that detected it (staticcheck, deadcode, goimports)
  4. Classification (remove / verify / false positive)
  5. Estimated lines saved

Summary: total findings, how many safe to remove, estimated cleanup size.

Thêm skills từ openshift

openshift-docs
openshift
Tìm kiếm và đọc tài liệu OpenShift Container Platform ở định dạng markdown. Sử dụng khi người dùng hỏi về các tính năng, cấu hình, cài đặt của OpenShift,…
triage-leaked-infra
openshift
Đánh giá xem VPC AWS hoặc bộ hạ tầng từ HyperShift CI có an toàn để xóa hay không. Sử dụng khi người dùng dán đầu ra cleanleaked và hỏi 'tôi có thể xóa cái này không?', 'có…
openshift-expert
openshift
Chuyên gia nền tảng OpenShift và Kubernetes với kiến thức sâu về kiến trúc cluster, operators, mạng, lưu trữ, xử lý sự cố và pipeline CI/CD. Sử dụng…
Konflux Archived PipelineRuns
openshift
Truy cập các PipelineRuns, TaskRuns và nhật ký pod Konflux đã lưu trữ thông qua KubeArchive. Tự động áp dụng khi kiểm tra kết quả PipelineRun Konflux, điều tra…
backport
openshift
Backport các commit hoặc PR từ nhánh main sang nhánh release. Sử dụng khi người dùng yêu cầu backport, cherry-pick, hoặc chuyển các thay đổi giữa các nhánh, hoặc khi đang xử lý…
rebase
openshift
Rebase nhánh hiện tại lên nhánh cơ sở của nó, giải quyết tất cả xung đột, và xác minh lint, i18n, và build đều thành công. Sử dụng khi người dùng yêu cầu rebase, cập nhật, hoặc đồng bộ…
Build CPO Image
openshift
Xây dựng và đẩy hình ảnh container control-plane-operator. Tự động áp dụng khi kiểm tra các thay đổi CPO yêu cầu triển khai lên cụm trực tiếp.
find-complexity
openshift
Tìm các hàm và phương thức có độ phức tạp cyclomatic cao, độ dài quá mức, hoặc quá nhiều tham số. Sử dụng khi người dùng yêu cầu tìm mã phức tạp, độ phức tạp…