find-dead-code

작성자: openshift

Find unused functions, types, constants, imports, and unreachable code paths. Use when the user asks to find dead code, unused code, cleanup candidates, or…

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.

openshift의 다른 스킬

openshift-expert
openshift
OpenShift 플랫폼 및 Kubernetes 전문가로, 클러스터 아키텍처, 오퍼레이터, 네트워킹, 스토리지, 문제 해결 및 CI/CD 파이프라인에 대한 깊은 지식을 보유하고 있습니다. 사용…
official
find-token
openshift
숨겨진 인증 토큰을 찾습니다. find-token 스크립트를 실행하여 고유 토큰을 검색하세요.
official
code-review
openshift
풀 리퀘스트의 코드 품질, 정확성, 프로젝트 규칙을 검토합니다. 사용자가 PR 검토, 코드 리뷰, 또는 변경 사항 확인을 요청할 때 사용하세요.
official
css-review
openshift
CSS 코딩 스타일, PatternFly 토큰 사용, 모범 사례를 검토합니다. 사용자가 CSS 검토, 스타일 확인, 또는 CSS 파일 감사를 요청할 때 사용하세요.
official
review-readmes
openshift
리포지토리의 모든 README.md 파일을 검토하여 오타, 오류, 오래된 정보를 찾습니다. 사용자가 README 검토, 문서 정확성 확인 등을 요청할 때 사용합니다.
official
review-skills
openshift
프로젝트 AI 스킬의 중복, 오래된 참조, 오류 및 구조적 문제를 검토합니다. 사용자가 스킬 검토, 스킬 감사, 확인 등을 요청할 때 사용하세요.
official
test
openshift
태그로 필터링된 종단 간 테스트를 실행합니다. 사용자가 테스트 실행, Playwright 실행, 또는 @core나 @attach 같은 특정 기능 태그를 테스트하도록 요청할 때 사용하세요.
official
unused-exports
openshift
다른 파일에서 가져오지 않은 내보낸 심볼을 찾습니다. 사용자가 "내보내기 확인", "사용되지 않는 내보내기"라고 말하거나 내보내기를 정리하도록 요청할 때 사용하세요.
official