From 9646482274d01f82b74200da5598b5964c2ea828 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Wed, 4 Mar 2020 11:39:36 -0800 Subject: [PATCH] Add staticcheck and fix issues This adds staticcheck as a linter to Zap and fixes issues found by it. Specifically, it found a few unused unexported types and functions. --- Makefile | 8 +++++++- go.mod | 1 + sugar_test.go | 4 ---- tools_test.go | 1 + zapcore/encoder_test.go | 8 -------- zapcore/error.go | 5 ----- 6 files changed, 9 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 21e436c45..dfaf6406e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ export GOBIN ?= $(shell pwd)/bin GOLINT = $(GOBIN)/golint +STATICCHECK = $(GOBIN)/staticcheck BENCH_FLAGS ?= -cpuprofile=cpu.pprof -memprofile=mem.pprof -benchmem # Directories containing independent Go modules. @@ -17,7 +18,7 @@ GO_FILES := $(shell \ all: lint test .PHONY: lint -lint: $(GOLINT) +lint: $(GOLINT) $(STATICCHECK) @rm -rf lint.log @echo "Checking formatting..." @gofmt -d -s $(GO_FILES) 2>&1 | tee lint.log @@ -25,6 +26,8 @@ lint: $(GOLINT) @$(foreach dir,$(MODULE_DIRS),(cd $(dir) && go vet ./... 2>&1) &&) true | tee -a lint.log @echo "Checking lint..." @$(foreach dir,$(MODULE_DIRS),(cd $(dir) && $(GOLINT) ./... 2>&1) &&) true | tee -a lint.log + @echo "Checking staticcheck..." + @$(foreach dir,$(MODULE_DIRS),(cd $(dir) && $(STATICCHECK) ./... 2>&1) &&) true | tee -a lint.log @echo "Checking for unresolved FIXMEs..." @git grep -i fixme | grep -v -e Makefile | tee -a lint.log @echo "Checking for license headers..." @@ -34,6 +37,9 @@ lint: $(GOLINT) $(GOLINT): go install golang.org/x/lint/golint +$(STATICCHECK): + go install honnef.co/go/tools/cmd/staticcheck + .PHONY: test test: @$(foreach dir,$(MODULE_DIRS),(cd $(dir) && go test -race ./...) &&) true diff --git a/go.mod b/go.mod index 8e974d826..118abda15 100644 --- a/go.mod +++ b/go.mod @@ -8,4 +8,5 @@ require ( go.uber.org/atomic v1.6.0 go.uber.org/multierr v1.5.0 golang.org/x/lint v0.0.0-20190930215403-16217165b5de + honnef.co/go/tools v0.0.1-2019.2.3 ) diff --git a/sugar_test.go b/sugar_test.go index 5b1dcfacd..5f4bb83b9 100644 --- a/sugar_test.go +++ b/sugar_test.go @@ -159,10 +159,6 @@ func TestSugarFieldsInvalidPairs(t *testing.T) { }) } -type stringerF func() string - -func (f stringerF) String() string { return f() } - func TestSugarStructuredLogging(t *testing.T) { tests := []struct { msg string diff --git a/tools_test.go b/tools_test.go index 2b6366bea..67e9f5801 100644 --- a/tools_test.go +++ b/tools_test.go @@ -25,4 +25,5 @@ package zap import ( // Tools we use during development. _ "golang.org/x/lint/golint" + _ "honnef.co/go/tools/cmd/staticcheck" ) diff --git a/zapcore/encoder_test.go b/zapcore/encoder_test.go index 3f521971a..54556b8d4 100644 --- a/zapcore/encoder_test.go +++ b/zapcore/encoder_test.go @@ -67,14 +67,6 @@ func humanEncoderConfig() EncoderConfig { return cfg } -func withJSONEncoder(f func(Encoder)) { - f(NewJSONEncoder(testEncoderConfig())) -} - -func withConsoleEncoder(f func(Encoder)) { - f(NewConsoleEncoder(humanEncoderConfig())) -} - func capitalNameEncoder(loggerName string, enc PrimitiveArrayEncoder) { enc.AppendString(strings.ToUpper(loggerName)) } diff --git a/zapcore/error.go b/zapcore/error.go index a67c7bacc..9ba2272c3 100644 --- a/zapcore/error.go +++ b/zapcore/error.go @@ -66,11 +66,6 @@ type errorGroup interface { Errors() []error } -type causer interface { - // Provides access to the error that caused this error. - Cause() error -} - // Note that errArry and errArrayElem are very similar to the version // implemented in the top-level error.go file. We can't re-use this because // that would require exporting errArray as part of the zapcore API.