aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2025-01-26 14:18:51 +0700
committerShulhan <ms@kilabit.info>2025-01-26 14:23:26 +0700
commitd205b6491ca4321b735748d27295d84584c006ac (patch)
tree68180bcebd1d862c0a3b5ef28255b018ac3a41f7
parent8d1e0fd8ec0179e9ad7ecc7a892d099729bf8dbf (diff)
downloadpakakeh.go-d205b6491ca4321b735748d27295d84584c006ac.tar.xz
lib/goanalysis: package to complement "go vet"
Package goanalysis implement go static analysis using [Analyzer] that are not included in the default "go vet", but included in the [passes] directory, including: fieldalignment, nilness, reflectvaluecompare, shadow, sortslice, unusedwrite, and waitgroup. This package is not mean to be imported directly by other package except main, like we have in [cmd/gocheck]. The rest (and previous) of the changes are affect running gocheck on it.
-rw-r--r--.gitignore1
-rw-r--r--Makefile3
-rw-r--r--cmd/gocheck/main.go17
-rw-r--r--lib/binary/apo_footer.go2
-rw-r--r--lib/crypto/crypto_test.go2
-rw-r--r--lib/goanalysis/goanalysis.go50
6 files changed, 71 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index 43d611bb..03d6f870 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,6 +19,7 @@
/_bin/bcrypt
/_bin/emaildecode
/_bin/epoch
+/_bin/gocheck
/_bin/gofmtcomment
/_bin/hexo
/_bin/httpdfs
diff --git a/Makefile b/Makefile
index af994b5a..6b032399 100644
--- a/Makefile
+++ b/Makefile
@@ -36,8 +36,7 @@ test.prof:
go test -race -timeout=1m -cpuprofile $(CPU_PROF) -memprofile $(MEM_PROF) ./...
lint:
- -fieldalignment ./...
- -shadow ./...
+ go run ./cmd/gocheck ./...
go vet ./...
$(CIIGO):
diff --git a/cmd/gocheck/main.go b/cmd/gocheck/main.go
new file mode 100644
index 00000000..cb9d3787
--- /dev/null
+++ b/cmd/gocheck/main.go
@@ -0,0 +1,17 @@
+// SPDX-FileCopyrightText: 2024 M. Shulhan <ms@kilabit.info>
+//
+// SPDX-License-Identifier: BSD-3-Clause
+
+// Program gocheck implement go static analysis using [Analyzer] that are not
+// included in the default go vet.
+// See package [lib/goanalysis] for more information.
+//
+// [Analyzer]: https://pkg.go.dev/golang.org/x/tools/go/analysis#hdr-Analyzer
+// [lib/goanalysis]: https://pkg.go.dev/git.sr.ht/~shulhan/pakakeh.go/lib/goanalysis/
+package main
+
+import "git.sr.ht/~shulhan/pakakeh.go/lib/goanalysis"
+
+func main() {
+ goanalysis.Check()
+}
diff --git a/lib/binary/apo_footer.go b/lib/binary/apo_footer.go
index 7e6670c1..17d7c08f 100644
--- a/lib/binary/apo_footer.go
+++ b/lib/binary/apo_footer.go
@@ -17,7 +17,7 @@ type apoFooter struct {
func (foot *apoFooter) WriteTo(w io.Writer) (n int64, err error) {
var nidx int64 = int64(len(foot.idxMetaOff))
- _ = binary.Write(w, binary.BigEndian, nidx)
+ err = binary.Write(w, binary.BigEndian, nidx)
if err != nil {
return 0, err
}
diff --git a/lib/crypto/crypto_test.go b/lib/crypto/crypto_test.go
index b99ff4a4..a90f42ca 100644
--- a/lib/crypto/crypto_test.go
+++ b/lib/crypto/crypto_test.go
@@ -173,7 +173,7 @@ func TestLoadPrivateKeyInteractive(t *testing.T) {
_, ok = pkey.(*rsa.PrivateKey)
if !ok {
- test.Assert(t, c.desc+` cast to *rsa.PrivateKey`, c.expError, err.Error())
+ t.Fatalf(`expecting pkey is *rsa.PrivateKey, got %T`, pkey)
continue
}
}
diff --git a/lib/goanalysis/goanalysis.go b/lib/goanalysis/goanalysis.go
new file mode 100644
index 00000000..25f13295
--- /dev/null
+++ b/lib/goanalysis/goanalysis.go
@@ -0,0 +1,50 @@
+// SPDX-FileCopyrightText: 2024 M. Shulhan <ms@kilabit.info>
+//
+// SPDX-License-Identifier: BSD-3-Clause
+
+// Package goanalysis implement go static analysis using
+// [Analyzer] that are not included in the default "go vet", but included in
+// the [passes] directory, including
+//
+// - fieldalignment: detects structs that would use less memory if their
+// fields were sorted.
+// - nilness: inspects the control-flow graph of an SSA function and reports
+// errors such as nil pointer dereferences and degenerate nil pointer
+// comparisons.
+// - reflectvaluecompare: checks for accidentally using == or
+// [reflect.DeepEqual] to compare reflect.Value values.
+// - shadow: checks for shadowed variables.
+// - sortslice: checks for calls to sort.Slice that do not use a slice type
+// as first argument.
+// - unusedwrite: checks for unused writes to the elements of a struct or
+// array object.
+// - waitgroup: detects simple misuses of sync.WaitGroup.
+//
+// [Analyzer]: https://pkg.go.dev/golang.org/x/tools/go/analysis#hdr-Analyzer
+// [passes]: https://pkg.go.dev/golang.org/x/tools/go/analysis/passes
+package goanalysis
+
+import (
+ "golang.org/x/tools/go/analysis/multichecker"
+ "golang.org/x/tools/go/analysis/passes/fieldalignment"
+ "golang.org/x/tools/go/analysis/passes/nilness"
+ "golang.org/x/tools/go/analysis/passes/reflectvaluecompare"
+ "golang.org/x/tools/go/analysis/passes/shadow"
+ "golang.org/x/tools/go/analysis/passes/sortslice"
+ "golang.org/x/tools/go/analysis/passes/unusedwrite"
+ "golang.org/x/tools/go/analysis/passes/waitgroup"
+)
+
+// Check run the static analysis.
+// This function is not mean to be call directly, but used in the main func.
+func Check() {
+ multichecker.Main(
+ fieldalignment.Analyzer,
+ nilness.Analyzer,
+ reflectvaluecompare.Analyzer,
+ shadow.Analyzer,
+ sortslice.Analyzer,
+ unusedwrite.Analyzer,
+ waitgroup.Analyzer,
+ )
+}