This commit is contained in:
Tomas Dvorak
2026-02-22 15:41:27 +01:00
parent 0b88627e54
commit 409acd2e08
84 changed files with 65382 additions and 27475 deletions
@@ -30,6 +30,31 @@ func (d *DeadCodeDetector) Severity() quality.Severity {
return quality.SeverityT2
}
func (d *DeadCodeDetector) shouldSkipExport(name, objType string) bool {
// Skip common API surface exports that might be used externally
skipPatterns := []string{
"Version", "License", "APIKey", "Config", "Options",
"Client", "Server", "Handler", "Service", "Manager",
"Store", "Cache", "Index", "Search", "Query",
}
// Skip type definitions and constants
if strings.Contains(objType, "type") ||
strings.Contains(objType, "const") ||
strings.Contains(objType, "var") {
return true
}
// Skip common naming patterns
for _, pattern := range skipPatterns {
if name == pattern {
return true
}
}
return false
}
func (d *DeadCodeDetector) Detect(ctx context.Context, path string, config *quality.Config) ([]quality.Finding, error) {
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedFiles,
@@ -58,18 +83,24 @@ func (d *DeadCodeDetector) Detect(ctx context.Context, path string, config *qual
continue
}
// Skip unexported objects - they're internal
if !obj.Exported() {
continue
}
key := obj.Pkg().Path() + "." + obj.Name()
if !used[key] {
// Skip certain types of exports that are commonly legitimate
if d.shouldSkipExport(obj.Name(), obj.Type().String()) {
continue
}
pos := pkg.Fset.Position(obj.Pos())
finding := quality.Finding{
ID: fmt.Sprintf("dead_code::%s::%s", pos.Filename, obj.Name()),
Type: "dead_code",
Title: fmt.Sprintf("Unused exported identifier: %s", obj.Name()),
Description: fmt.Sprintf("The exported %s '%s' is never used in the codebase. Consider removing it or documenting its intended use.", obj.Type(), obj.Name()),
Description: fmt.Sprintf("The exported %s '%s' is never used in codebase. Consider removing it or documenting its intended use.", obj.Type(), obj.Name()),
File: pos.Filename,
Line: pos.Line,
Severity: quality.SeverityT2,
@@ -290,7 +321,6 @@ func (d *CycleDetector) findCycles(graph map[string][]string) [][]string {
}
}
path = path[:len(path)-1]
recStack[node] = false
}