From bcd5efa0efcbda3fb95df5ea5b83a1ec07ab43b6 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Sun, 28 Dec 2014 08:35:52 -0800 Subject: [PATCH] go/types: make import check optional via a flag in types.Config It is sometimes useful for API clients to be able to disable the unused import check, although for good reason this should not be exposed as part of a user-facing interface. There are at least two use cases that I am aware of: 1) It allows for automated test case reduction tools such as delta or C-Reduce to be more easily applied to type checker input. Disabling the check makes it possible for the tool to identify and remove code that depends on imported packages without any specific knowledge of Go, as the import need not be removed simultaneously with the code. 2) Interactive tools (such as REPLs) that may have previously received a list of imports and subsequently receive a line of code that may use any number of these imports. It is simpler for such tools to import all the packages in its list than to try to identify the correct set of imports. Change-Id: I00091a4e5c8e1bd664efd82a636f255eaaa5a2db Reviewed-on: https://go-review.googlesource.com/2136 Reviewed-by: Robert Griesemer --- go/types/api.go | 4 ++++ go/types/check.go | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/go/types/api.go b/go/types/api.go index ae53f7c9..5344a39e 100644 --- a/go/types/api.go +++ b/go/types/api.go @@ -113,6 +113,10 @@ type Config struct { // If Sizes != nil, it provides the sizing functions for package unsafe. // Otherwise &StdSizes{WordSize: 8, MaxAlign: 8} is used instead. Sizes Sizes + + // If DisableUnusedImportCheck is set, packages are not checked + // for unused imports. + DisableUnusedImportCheck bool } // DefaultImport is the default importer invoked if Config.Import == nil. diff --git a/go/types/check.go b/go/types/check.go index 5b30a300..b48a8abf 100644 --- a/go/types/check.go +++ b/go/types/check.go @@ -233,7 +233,9 @@ func (check *Checker) Files(files []*ast.File) (err error) { check.initOrder() - check.unusedImports() + if !check.conf.DisableUnusedImportCheck { + check.unusedImports() + } // perform delayed checks for _, f := range check.delayed {