-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_unused_def_params.go
100 lines (88 loc) · 1.99 KB
/
check_unused_def_params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2021 The June Authors. All rights reserved.
// Use of this source code is governed by Apache License
// that can be found in the LICENSE file.
package juneqa
import (
"fmt"
"go/ast"
"go/types"
"github.com/reusee/qa"
"golang.org/x/tools/go/packages"
)
func (_ Def) CheckUnusedDefParams(
pkgs []*packages.Package,
) qa.CheckFunc {
return func() (ret []error) {
// sys.Def type
var sysDefType types.Type
for _, pkg := range pkgs {
if pkg.Name != "sys" {
continue
}
sysDefType = pkg.Types.Scope().Lookup("Def").(*types.TypeName).Type()
}
if sysDefType == nil {
panic("sys.Def not found")
}
// find unused injected params
for _, pkg := range pkgs {
for _, file := range pkg.Syntax {
for _, decl := range file.Decls {
fnDecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
obj := pkg.TypesInfo.Defs[fnDecl.Name]
sig := obj.Type().(*types.Signature)
params := sig.Params()
if params.Len() == 0 {
continue
}
if params.At(0).Type() != sysDefType {
continue
}
args := fnDecl.Type.Params.List
type Obj struct {
Ident *ast.Ident
Obj types.Object
}
objs := make(map[types.Object]Obj)
for _, arg := range args[1:] {
for _, ident := range arg.Names {
obj := pkg.TypesInfo.Defs[ident]
objs[obj] = Obj{
Ident: ident,
Obj: obj,
}
}
}
ast.Inspect(fnDecl.Body, func(node ast.Node) bool {
ident, ok := node.(*ast.Ident)
if !ok {
return true
}
obj := pkg.TypesInfo.Uses[ident]
if obj == nil {
return true
}
delete(objs, obj)
return true
})
for _, obj := range objs {
if obj.Ident.Name == "_" {
continue
}
pos := pkg.Fset.Position(obj.Ident.Pos())
ret = append(ret, fmt.Errorf(
"unused param: %s at %s:%d",
obj.Ident.Name,
pos.Filename,
pos.Line,
))
}
}
}
}
return
}
}