My answer is to hack the f-ing sources. This patch works against 1.19.4 and then you build your sources with -gcflags all=-nounusederrors
(you'll need help elsewhere in order to build Golang from sources):
tty/tty.go:98:6: hello declared but not used, but nobody cares
It doesn't suppress some unused labels and the grammar of the appended message is a bit sloppy, but nobody cares.
From 6eb19713fb5302ef2d5eb4af0c05e86c88d055c7 Mon Sep 17 00:00:00 2001
From: Daniel Santos <[email protected]>
Date: Mon, 9 Jan 2023 21:56:03 -0600
Subject: Add -nounusedwarnings
---
src/cmd/compile/internal/base/flag.go | 1 +
src/cmd/compile/internal/types2/errors.go | 10 ++++++++++
src/cmd/compile/internal/types2/labels.go | 2 +-
src/cmd/compile/internal/types2/resolver.go | 8 ++++----
src/cmd/compile/internal/types2/stmt.go | 4 ++--
src/cmd/go/alldocs.go | 2 ++
src/cmd/go/internal/work/build.go | 2 ++
src/go/types/gotype.go | 3 +++
8 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/src/cmd/compile/internal/base/flag.go b/src/cmd/compile/internal/base/flag.go
index a363b83984..f295746f64 100644
--- a/src/cmd/compile/internal/base/flag.go
+++ b/src/cmd/compile/internal/base/flag.go
@@ -111,6 +111,7 @@ type CmdFlags struct {
MemProfileRate int "help:\"set runtime.MemProfileRate to `rate`\""
MutexProfile string "help:\"write mutex profile to `file`\""
NoLocalImports bool "help:\"reject local (relative) imports\""
+ NoUnusedErrors bool "help:\"no errors for unused imports and variables\""
Pack bool "help:\"write to file.a instead of file.o\""
Race bool "help:\"enable race detector\""
Shared *bool "help:\"generate code that can be linked into a shared library\"" // &Ctxt.Flag_shared, set below
diff --git a/src/cmd/compile/internal/types2/errors.go b/src/cmd/compile/internal/types2/errors.go
index 2a3e88a2fe..0405fa26de 100644
--- a/src/cmd/compile/internal/types2/errors.go
+++ b/src/cmd/compile/internal/types2/errors.go
@@ -8,6 +8,7 @@ package types2
import (
"bytes"
+ "cmd/compile/internal/base"
"cmd/compile/internal/syntax"
"fmt"
"runtime"
@@ -275,6 +276,15 @@ func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
check.err(at, check.sprintf(format, args...), true)
}
+func (check *Checker) unusedf(at poser, format string, args ...interface{}) {
+ if base.Flag.NoUnusedErrors {
+ pos := posFor(at)
+ fmt.Printf("%s: %s, but nobody cares\n", pos, check.sprintf(format, args...))
+ } else {
+ check.softErrorf(at, format, args)
+ }
+}
+
func (check *Checker) versionErrorf(at poser, goVersion string, format string, args ...interface{}) {
msg := check.sprintf(format, args...)
if check.conf.CompilerErrorMessages {
diff --git a/src/cmd/compile/internal/types2/labels.go b/src/cmd/compile/internal/types2/labels.go
index 6f02e2fc96..d3ae602549 100644
--- a/src/cmd/compile/internal/types2/labels.go
+++ b/src/cmd/compile/internal/types2/labels.go
@@ -35,7 +35,7 @@ func (check *Checker) labels(body *syntax.BlockStmt) {
for name, obj := range all.elems {
obj = resolve(name, obj)
if lbl := obj.(*Label); !lbl.used {
- check.softErrorf(lbl.pos, "label %s declared but not used", lbl.name)
+ check.unusedf(lbl.pos, "label %s declared but not used", lbl.name)
}
}
}
diff --git a/src/cmd/compile/internal/types2/resolver.go b/src/cmd/compile/internal/types2/resolver.go
index 5d498b6b2b..935435b03f 100644
--- a/src/cmd/compile/internal/types2/resolver.go
+++ b/src/cmd/compile/internal/types2/resolver.go
@@ -731,15 +731,15 @@ func (check *Checker) errorUnusedPkg(obj *PkgName) {
}
if obj.name == "" || obj.name == "." || obj.name == elem {
if check.conf.CompilerErrorMessages {
- check.softErrorf(obj, "imported and not used: %q", path)
+ check.unusedf(obj, "imported and not used: %q", path)
} else {
- check.softErrorf(obj, "%q imported but not used", path)
+ check.unusedf(obj, "%q imported but not used", path)
}
} else {
if check.conf.CompilerErrorMessages {
- check.softErrorf(obj, "imported and not used: %q as %s", path, obj.name)
+ check.unusedf(obj, "imported and not used: %q as %s", path, obj.name)
} else {
- check.softErrorf(obj, "%q imported but not used as %s", path, obj.name)
+ check.unusedf(obj, "%q imported but not used as %s", path, obj.name)
}
}
}
diff --git a/src/cmd/compile/internal/types2/stmt.go b/src/cmd/compile/internal/types2/stmt.go
index 74d4164ba9..c4255e4413 100644
--- a/src/cmd/compile/internal/types2/stmt.go
+++ b/src/cmd/compile/internal/types2/stmt.go
@@ -66,7 +66,7 @@ func (check *Checker) usage(scope *Scope) {
return unused[i].pos.Cmp(unused[j].pos) < 0
})
for _, v := range unused {
- check.softErrorf(v.pos, "%s declared but not used", v.name)
+ check.unusedf(v.pos, "%s declared but not used", v.name)
}
for _, scope := range scope.children {
@@ -804,7 +804,7 @@ func (check *Checker) typeSwitchStmt(inner stmtContext, s *syntax.SwitchStmt, gu
v.used = true // avoid usage error when checking entire function
}
if !used {
- check.softErrorf(lhs, "%s declared but not used", lhs.Value)
+ check.unusedf(lhs, "%s declared but not used", lhs.Value)
}
}
}
diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go
index a3c1fecb91..1f4c5c7b5c 100644
--- a/src/cmd/go/alldocs.go
+++ b/src/cmd/go/alldocs.go
@@ -179,6 +179,8 @@
// directory, but it is not accessed. When -modfile is specified, an
// alternate go.sum file is also used: its path is derived from the
// -modfile flag by trimming the ".mod" extension and appending ".sum".
+// -nounusederrors
+// do not error on unused functions, imports, variables, etc.
// -overlay file
// read a JSON config file that provides an overlay for build operations.
// The file is a JSON struct with a single field, named 'Replace', that
diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go
index 5f11cdabaf..b37f1c8a01 100644
--- a/src/cmd/go/internal/work/build.go
+++ b/src/cmd/go/internal/work/build.go
@@ -135,6 +135,8 @@ and test commands:
directory, but it is not accessed. When -modfile is specified, an
alternate go.sum file is also used: its path is derived from the
-modfile flag by trimming the ".mod" extension and appending ".sum".
+ -nounusederrors
+ do not error on unused functions, imports, variables, etc.
-overlay file
read a JSON config file that provides an overlay for build operations.
The file is a JSON struct with a single field, named 'Replace', that
diff --git a/src/go/types/gotype.go b/src/go/types/gotype.go
index e8ff9658da..5a60b83346 100644
--- a/src/go/types/gotype.go
+++ b/src/go/types/gotype.go
@@ -47,6 +47,8 @@ The flags are:
verbose mode
-c
compiler used for installed packages (gc, gccgo, or source); default: source
+ -nounusederrors
+ treat "unused" errors as warnings
Flags controlling additional output:
@@ -104,6 +106,7 @@ var (
allErrors = flag.Bool("e", false, "report all errors, not just the first 10")
verbose = flag.Bool("v", false, "verbose mode")
compiler = flag.String("c", "source", "compiler used for installed packages (gc, gccgo, or source)")
+ nounusederr= flag.Bool("nounusederrors", false, "treat unused objects as warnings")
// additional output control
printAST = flag.Bool("ast", false, "print AST")
--
2.38.2
Disclaimer: this isn't a comprehensive patch -- it only affects running go build
or the compile
tool. Should still have errors when using ast
(tree parser), tracing and a few others tools because I didn't mess with internal/types
, only cmd/compile/internal/types2
-- the Golang sources are a bit messy like that. I'm hoping they can refactor at some point and get rid of these redundancies.
_
is still a very cumbersome work around which demonstrably breaks developer flow. Every single time you comment something out you then need to go and stub all thedeclared but not used
errors. Any team could implement standards and style checks as a commit hook or part of a CI/Gate job. The lack of agency given to developers here is patronizing and furthermore forces us to write worse code in order to be able to pull things apart.goimports
solves the issue for imports, a compiler flag should be acceptable for variables.