@@ -48,7 +48,8 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
4848 inspect := pass .ResultOf [inspect .Analyzer ].(* inspector.Inspector )
4949
5050 // 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
5253
5354 nodeFilter := []ast.Node {
5455 (* ast .GenDecl )(nil ),
@@ -80,7 +81,7 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
8081
8182 _ , ok = ts .Type .(* ast.InterfaceType )
8283 if ! ok {
83- return
84+ continue
8485 }
8586
8687 if r .debug {
@@ -93,7 +94,8 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
9394 continue
9495 }
9596
96- ifaceDecls [ts .Name .Name ] = ts .Pos ()
97+ ifaceDecls [ts .Name .Name ] = ts
98+ genDecls [ts .Name .Name ] = decl
9799 }
98100 })
99101
@@ -117,21 +119,51 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
117119 return
118120 }
119121
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 () {
122128 // The identifier is the interface type declaration
123129 return
124130 }
125131
126132 delete (ifaceDecls , ident .Name )
133+ delete (genDecls , ident .Name )
127134 })
128135
129136 if r .debug {
130137 fmt .Printf ("Package %s %s\n " , pass .Pkg .Path (), pass .Pkg .Name ())
131138 }
132139
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+ })
135167 }
136168
137169 return nil , nil
0 commit comments