diff --git a/main.go b/main.go index 4b5ca3b..6e0803c 100644 --- a/main.go +++ b/main.go @@ -1493,9 +1493,7 @@ func (tf *transformer) removeUnnecessaryImports(file *ast.File) { continue } - if !astutil.DeleteImport(fset, file, path) { - panic(fmt.Sprintf("cannot delete unused import: %q", path)) - } + imp.Name = ast.NewIdent("_") } } diff --git a/testdata/mod/gopkg.in_garbletestconst.v2_v2.999.0.txt b/testdata/mod/gopkg.in_garbletestconst.v2_v2.999.0.txt new file mode 100644 index 0000000..c719af2 --- /dev/null +++ b/testdata/mod/gopkg.in_garbletestconst.v2_v2.999.0.txt @@ -0,0 +1,32 @@ +module gopkg.in/garbletestconst.v2@v2.999.0 + +-- .mod -- +module gopkg.in/garbletestconst.v2 + +go 1.17 + +-- .info -- +{"Version":"v2.999.0","Time":"2020-11-17T15:46:20Z"} +-- go.mod -- +module gopkg.in/garbletestconst.v2 + +go 1.17 +-- consts.go -- +package garbletestconst + +import ( + "database/sql" + "database/sql/driver" +) + +const StrConst = "42" + +func init() { + sql.Register("dummy", dummy{}) +} + +type dummy struct{} + +func (dummy) Open(string) (driver.Conn, error) { + panic("not implemented") +} diff --git a/testdata/scripts/imports.txt b/testdata/scripts/imports.txt index 008d3cd..d588799 100644 --- a/testdata/scripts/imports.txt +++ b/testdata/scripts/imports.txt @@ -49,6 +49,7 @@ go 1.18 require ( gopkg.in/garbletest.v2 v2.999.0 + gopkg.in/garbletestconst.v2 v2.999.0 rsc.io/quote v1.5.2 ) @@ -62,6 +63,8 @@ golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:pvCbr/wm8HzDD3fVywevekuf golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/garbletest.v2 v2.999.0 h1:XHlBQi3MAcJL2fjNiEPAPAilkzc7hAv4vyyjY5w+IUY= gopkg.in/garbletest.v2 v2.999.0/go.mod h1:MI9QqKJD8i8oL8mW/bR0qq19/VuezEdJbVvl2B8Pa40= +gopkg.in/garbletestconst.v2 v2.999.0 h1:VABqc63EJolbOSh1+WlSSQmX8ZT7VIs53mKCGxECTH8= +gopkg.in/garbletestconst.v2 v2.999.0/go.mod h1:QA2FI8zGZhhsdLJGUrq78ah+ohEZo9ZDw3ex+C2WVEc= rsc.io/quote v1.5.2 h1:3fEykkD9k7lYzXqCYrwGAf7iNhbk4yCjHmKBN9td4L0= rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0= rsc.io/sampler v1.3.0 h1:+lXbM7nYGGOYhnMEiMtjCwcUfjn4sajeMm15HMT6SnU= @@ -73,11 +76,13 @@ package main import ( "fmt" + "database/sql" "test/main/importedpkg" "rsc.io/quote" garbletest "gopkg.in/garbletest.v2" + garbletestconst "gopkg.in/garbletestconst.v2" ) func main() { @@ -90,6 +95,8 @@ func main() { fmt.Println(quote.Go()) garbletest.Test() + fmt.Println(garbletestconst.StrConst) + fmt.Println(sql.Drivers()[0]) } -- notag_fail.go -- // +build !buildtag @@ -159,3 +166,5 @@ imported const value x {3 {23} 0} Don't communicate by sharing memory, share memory by communicating. +42 +dummy \ No newline at end of file