@@ -48,7 +48,8 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
48
48
inspect := pass .ResultOf [inspect .Analyzer ].(* inspector.Inspector )
49
49
50
50
// Collect all interface type declarations
51
- ifaceDecls := make (map [string ]token.Pos )
51
+ ifaceDecls := make (map [string ]* ast.TypeSpec )
52
+ genDecls := make (map [string ]* ast.GenDecl ) // ifaceName -> GenDecl
52
53
53
54
nodeFilter := []ast.Node {
54
55
(* ast .GenDecl )(nil ),
@@ -80,7 +81,7 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
80
81
81
82
_ , ok = ts .Type .(* ast.InterfaceType )
82
83
if ! ok {
83
- return
84
+ continue
84
85
}
85
86
86
87
if r .debug {
@@ -93,7 +94,8 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
93
94
continue
94
95
}
95
96
96
- ifaceDecls [ts .Name .Name ] = ts .Pos ()
97
+ ifaceDecls [ts .Name .Name ] = ts
98
+ genDecls [ts .Name .Name ] = decl
97
99
}
98
100
})
99
101
@@ -117,21 +119,51 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
117
119
return
118
120
}
119
121
120
- pos := ifaceDecls [ident .Name ]
121
- if pos == ident .Pos () {
122
+ ts , ok := ifaceDecls [ident .Name ]
123
+ if ! ok {
124
+ return
125
+ }
126
+
127
+ if ts .Pos () == ident .Pos () {
122
128
// The identifier is the interface type declaration
123
129
return
124
130
}
125
131
126
132
delete (ifaceDecls , ident .Name )
133
+ delete (genDecls , ident .Name )
127
134
})
128
135
129
136
if r .debug {
130
137
fmt .Printf ("Package %s %s\n " , pass .Pkg .Path (), pass .Pkg .Name ())
131
138
}
132
139
133
- for name , pos := range ifaceDecls {
134
- pass .Reportf (pos , "interface %s is declared but not used within the package" , name )
140
+ for name , ts := range ifaceDecls {
141
+ decl := genDecls [name ]
142
+
143
+ var node ast.Node
144
+ if len (decl .Specs ) == 1 {
145
+ node = decl
146
+ } else {
147
+ node = ts
148
+ }
149
+
150
+ msg := fmt .Sprintf ("interface %s is declared but not used within the package" , name )
151
+ pass .Report (analysis.Diagnostic {
152
+ Pos : ts .Pos (),
153
+ Message : msg ,
154
+ SuggestedFixes : []analysis.SuggestedFix {
155
+ {
156
+ Message : "Remove the unused interface declaration" ,
157
+ TextEdits : []analysis.TextEdit {
158
+ {
159
+ Pos : node .Pos (),
160
+ End : node .End (),
161
+ NewText : []byte {},
162
+ },
163
+ },
164
+ },
165
+ },
166
+ })
135
167
}
136
168
137
169
return nil , nil
0 commit comments