Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: fix govet-shadow lint #16608

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ fuzz:

verify: verify-gofmt verify-bom verify-lint verify-dep verify-shellcheck verify-goword \
verify-govet verify-license-header verify-receiver-name verify-mod-tidy verify-shellcheck \
verify-shellws verify-proto-annotations verify-genproto verify-goimport verify-yamllint
verify-shellws verify-proto-annotations verify-genproto verify-goimport verify-yamllint \
verify-govet-shadow
fix: fix-goimports fix-bom fix-lint fix-yamllint
./scripts/fix.sh

Expand Down Expand Up @@ -140,6 +141,10 @@ fix-goimports:
verify-yamllint:
yamllint --config-file tools/.yamllint .

.PHONY: verify-govet-shadow
verify-govet-shadow:
PASSES="govet_shadow" ./scripts/test.sh

YAMLFMT_VERSION = $(shell cd tools/mod && go list -m -f '{{.Version}}' github.com/google/yamlfmt)

.PHONY: fix-yamllint
Expand Down
4 changes: 2 additions & 2 deletions client/v3/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@ func (l *lessor) keepAliveOnce(ctx context.Context, id LeaseID) (karesp *LeaseKe
}

defer func() {
if err := stream.CloseSend(); err != nil {
if cerr := stream.CloseSend(); cerr != nil {
if ferr == nil {
ferr = toErr(ctx, err)
ferr = toErr(ctx, cerr)
}
return
}
Expand Down
11 changes: 6 additions & 5 deletions client/v3/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,19 @@ func (m *maintenance) SnapshotWithVersion(ctx context.Context) (*SnapshotRespons
}
go func() {
// Saving response is blocking
err = m.save(resp, pw)
err := m.save(resp, pw)
if err != nil {
m.logAndCloseWithError(err, pw)
return
}
for {
resp, err := ss.Recv()
sresp, err := ss.Recv()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use new sresp here because the old name shadows the L240.

./maintenance.go:253:4: declaration of "resp" shadows declaration at line 240

if err != nil {
m.logAndCloseWithError(err, pw)
return
}
err = m.save(resp, pw)

err = m.save(sresp, pw)
if err != nil {
m.logAndCloseWithError(err, pw)
return
Expand All @@ -267,7 +268,7 @@ func (m *maintenance) SnapshotWithVersion(ctx context.Context) (*SnapshotRespons
Header: resp.GetHeader(),
Snapshot: &snapshotReadCloser{ctx: ctx, ReadCloser: pr},
Version: resp.GetVersion(),
}, err
}, nil
}

func (m *maintenance) Snapshot(ctx context.Context) (io.ReadCloser, error) {
Expand All @@ -293,7 +294,7 @@ func (m *maintenance) Snapshot(ctx context.Context) (io.ReadCloser, error) {
}
}
}()
return &snapshotReadCloser{ctx: ctx, ReadCloser: pr}, err
return &snapshotReadCloser{ctx: ctx, ReadCloser: pr}, nil
}

func (m *maintenance) logAndCloseWithError(err error, pw *io.PipeWriter) {
Expand Down
12 changes: 6 additions & 6 deletions etcdutl/snapshot/v3_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@ func (s *v3Manager) Status(dbPath string) (ds Status, err error) {
if b == nil {
return fmt.Errorf("cannot get hash of bucket %s", string(next))
}
if _, err := h.Write(next); err != nil {
if _, err = h.Write(next); err != nil {
return fmt.Errorf("cannot write bucket %s : %v", string(next), err)
}
iskeyb := (string(next) == "key")
if err := b.ForEach(func(k, v []byte) error {
if _, err := h.Write(k); err != nil {
return fmt.Errorf("cannot write to bucket %s", err.Error())
if err = b.ForEach(func(k, v []byte) error {
if _, herr := h.Write(k); herr != nil {
return fmt.Errorf("cannot write to bucket %s", herr.Error())
}
if _, err := h.Write(v); err != nil {
return fmt.Errorf("cannot write to bucket %s", err.Error())
if _, herr := h.Write(v); herr != nil {
return fmt.Errorf("cannot write to bucket %s", herr.Error())
}
if iskeyb {
rev := bytesToRev(k)
Expand Down
7 changes: 3 additions & 4 deletions pkg/expect/expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func TestExpectFuncTimeout(t *testing.T) {
go func() {
// It's enough to have "talkative" process to stuck in the infinite loop of reading
for {
err := ep.Send("new line\n")
if err != nil {
if serr := ep.Send("new line\n"); serr != nil {
return
}
}
Expand All @@ -67,7 +66,7 @@ func TestExpectFuncTimeout(t *testing.T) {

require.ErrorAs(t, err, &context.DeadlineExceeded)

if err := ep.Stop(); err != nil {
if err = ep.Stop(); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -111,7 +110,7 @@ func TestExpectFuncExitFailureStop(t *testing.T) {
require.Equal(t, 1, exitCode)
require.NoError(t, err)

if err := ep.Stop(); err != nil {
if err = ep.Stop(); err != nil {
t.Fatal(err)
}
err = ep.Close()
Expand Down
20 changes: 10 additions & 10 deletions pkg/traceutil/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,33 +181,33 @@ func (t *Trace) logInfo(threshold time.Duration) (string, []zap.Field) {
var steps []string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Line 207 is still using it~

Copy link
Member

@serathius serathius Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

step, steps, tstep, t.steps.

Think this function needs a for major redo.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use tstep here based on t.steps. I mark it as TODO if need.

lastStepTime := t.startTime
for i := 0; i < len(t.steps); i++ {
step := t.steps[i]
tstep := t.steps[i]
// add subtrace common fields which defined at the beginning to each sub-steps
if step.isSubTraceStart {
if tstep.isSubTraceStart {
for j := i + 1; j < len(t.steps) && !t.steps[j].isSubTraceEnd; j++ {
t.steps[j].fields = append(step.fields, t.steps[j].fields...)
t.steps[j].fields = append(tstep.fields, t.steps[j].fields...)
}
continue
}
// add subtrace common fields which defined at the end to each sub-steps
if step.isSubTraceEnd {
if tstep.isSubTraceEnd {
for j := i - 1; j >= 0 && !t.steps[j].isSubTraceStart; j-- {
t.steps[j].fields = append(step.fields, t.steps[j].fields...)
t.steps[j].fields = append(tstep.fields, t.steps[j].fields...)
}
continue
}
}
for i := 0; i < len(t.steps); i++ {
step := t.steps[i]
if step.isSubTraceStart || step.isSubTraceEnd {
tstep := t.steps[i]
if tstep.isSubTraceStart || tstep.isSubTraceEnd {
continue
}
stepDuration := step.time.Sub(lastStepTime)
stepDuration := tstep.time.Sub(lastStepTime)
if stepDuration > threshold {
steps = append(steps, fmt.Sprintf("trace[%d] '%v' %s (duration: %v)",
traceNum, step.msg, writeFields(step.fields), stepDuration))
traceNum, tstep.msg, writeFields(tstep.fields), stepDuration))
}
lastStepTime = step.time
lastStepTime = tstep.time
}

fs := []zap.Field{zap.String("detail", writeFields(t.fields)),
Expand Down
38 changes: 35 additions & 3 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,45 @@ function govet_pass {
run_for_modules generic_checker run go vet
}

function govet_shadow_pass {
# TODO: we should ignore the generated packages?
function govet_shadow_per_package {
local shadow
shadow=$1

# skip grpc_gateway packages because
#
# stderr: etcdserverpb/gw/rpc.pb.gw.go:2100:3: declaration of "ctx" shadows declaration at line 2005
local skip_pkgs=(
"go.etcd.io/etcd/api/v3/etcdserverpb/gw"
"go.etcd.io/etcd/server/v3/etcdserver/api/v3lock/v3lockpb/gw"
"go.etcd.io/etcd/server/v3/etcdserver/api/v3election/v3electionpb/gw"
)

local pkgs=()
while IFS= read -r line; do
local in_skip_pkgs="false"

for pkg in "${skip_pkgs[@]}"; do
if [ "${pkg}" == "${line}" ]; then
in_skip_pkgs="true"
break
fi
done

if [ "${in_skip_pkgs}" == "true" ]; then
continue
fi

pkgs+=("${line}")
done < <(go list ./...)

run go vet -all -vettool="${shadow}" "${pkgs[@]}"
}

function govet_shadow_pass {
local shadow
shadow=$(tool_get_bin "golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow")
run_for_modules generic_checker run go vet -all -vettool="${shadow}"

run_for_modules generic_checker govet_shadow_per_package "${shadow}"
}

function unparam_pass {
Expand Down
8 changes: 4 additions & 4 deletions server/embed/config_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (logRotationConfig) Sync() error { return nil }

// setupLogRotation initializes log rotation for a single file path target.
func setupLogRotation(logOutputs []string, logRotateConfigJSON string) error {
var logRotationConfig logRotationConfig
var logRotationCfg logRotationConfig
outputFilePaths := 0
for _, v := range logOutputs {
switch v {
Expand All @@ -265,7 +265,7 @@ func setupLogRotation(logOutputs []string, logRotateConfigJSON string) error {
return ErrLogRotationInvalidLogOutput
}

if err := json.Unmarshal([]byte(logRotateConfigJSON), &logRotationConfig); err != nil {
if err := json.Unmarshal([]byte(logRotateConfigJSON), &logRotationCfg); err != nil {
var unmarshalTypeError *json.UnmarshalTypeError
var syntaxError *json.SyntaxError
switch {
Expand All @@ -278,8 +278,8 @@ func setupLogRotation(logOutputs []string, logRotateConfigJSON string) error {
}
}
zap.RegisterSink("rotate", func(u *url.URL) (zap.Sink, error) {
logRotationConfig.Filename = u.Path[1:]
return &logRotationConfig, nil
logRotationCfg.Filename = u.Path[1:]
return &logRotationCfg, nil
})
return nil
}
6 changes: 3 additions & 3 deletions server/embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {

if srvcfg.ExperimentalEnableDistributedTracing {
tctx := context.Background()
tracingExporter, err := newTracingExporter(tctx, cfg)
if err != nil {
return e, err
tracingExporter, terr := newTracingExporter(tctx, cfg)
if terr != nil {
return e, terr
}
e.tracingExporterShutdown = func() {
tracingExporter.Close(tctx)
Expand Down
2 changes: 1 addition & 1 deletion server/embed/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (sctx *serveCtx) serve(
Handler: createAccessController(sctx.lg, s, httpmux),
ErrorLog: logger, // do not log user error
}
if err := configureHttpServer(srv, s.Cfg); err != nil {
if err = configureHttpServer(srv, s.Cfg); err != nil {
sctx.lg.Error("Configure http server failed", zap.Error(err))
return err
}
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/api/v3compactor/periodic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func TestPeriodicSkipRevNotChange(t *testing.T) {
fc.Advance(tb.getRetryInterval())
}

_, err := compactable.Wait(1)
_, err = compactable.Wait(1)
if err == nil {
t.Fatal(errors.New("should not compact since the revision not change"))
}
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/api/v3discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (d *discovery) joinCluster(config string) (string, error) {
return "", err
}

if err := d.registerSelf(config); err != nil {
if err = d.registerSelf(config); err != nil {
return "", err
}

Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestBootstrapBackend(t *testing.T) {
}

if tt.prepareData != nil {
if err := tt.prepareData(cfg); err != nil {
if err = tt.prepareData(cfg); err != nil {
t.Fatalf("failed to prepare data, unexpected error: %v", err)
}
}
Expand Down
6 changes: 4 additions & 2 deletions server/etcdserver/corrupt.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,10 @@ func (s *EtcdServer) getPeerHashKVs(rev int64) []*peerHashKVResp {
respsLen := len(resps)
var lastErr error
for _, ep := range p.eps {
var resp *pb.HashKVResponse

ctx, cancel := context.WithTimeout(context.Background(), s.Cfg.ReqTimeout())
resp, lastErr := HashByRev(ctx, s.cluster.ID(), cc, ep, rev)
resp, lastErr = HashByRev(ctx, s.cluster.ID(), cc, ep, rev)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create new var to prevent shadow the lastErr.

etcdserver/corrupt.go:481:10: declaration of "lastErr" shadows declaration at line 478

cancel()
if lastErr == nil {
resps = append(resps, &peerHashKVResp{peerInfo: p, resp: resp, err: nil})
Expand Down Expand Up @@ -535,7 +537,7 @@ func (h *hashKVHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

req := &pb.HashKVRequest{}
if err := json.Unmarshal(b, req); err != nil {
if err = json.Unmarshal(b, req); err != nil {
h.lg.Warn("failed to unmarshal request", zap.Error(err))
http.Error(w, "error unmarshalling request", http.StatusBadRequest)
return
Expand Down
4 changes: 3 additions & 1 deletion server/storage/wal/testing/waltesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func NewTmpWAL(t testing.TB, reqs []etcdserverpb.InternalRaftRequest) (*wal.WAL,
if err != nil {
t.Fatalf("Failed to open WAL: %v", err)
}
_, state, _, err := w.ReadAll()

var state raftpb.HardState
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating new var state to reduce the line changes. err doesn't reuse in other goroutines. using err here might be more easy to review.

_, state, _, err = w.ReadAll()
if err != nil {
t.Fatalf("Failed to read WAL: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion server/storage/wal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func visitMessageDescriptor(md protoreflect.MessageDescriptor, visitor Visitor)

enums := md.Enums()
for i := 0; i < enums.Len(); i++ {
err := visitEnumDescriptor(enums.Get(i), visitor)
err = visitEnumDescriptor(enums.Get(i), visitor)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common/alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestAlarm(t *testing.T) {
}

// check that Put is rejected when alarm is on
if err := cc.Put(ctx, "3rd_test", smallbuf, config.PutOptions{}); err != nil {
if err = cc.Put(ctx, "3rd_test", smallbuf, config.PutOptions{}); err != nil {
if !strings.Contains(err.Error(), "etcdserver: mvcc: database space exceeded") {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestInPlaceRecovery(t *testing.T) {
oldCc, err := e2e.NewEtcdctl(epcOld.Cfg.Client, epcOld.EndpointsGRPC())
assert.NoError(t, err)
for i := 0; i < 10; i++ {
err := oldCc.Put(ctx, testutil.PickKey(int64(i)), fmt.Sprint(i), config.PutOptions{})
err = oldCc.Put(ctx, testutil.PickKey(int64(i)), fmt.Sprint(i), config.PutOptions{})
assert.NoError(t, err, "error on put")
}

Expand Down Expand Up @@ -203,7 +203,7 @@ func TestPeriodicCheckDetectsCorruption(t *testing.T) {

cc := epc.Etcdctl()
for i := 0; i < 10; i++ {
err := cc.Put(ctx, testutil.PickKey(int64(i)), fmt.Sprint(i), config.PutOptions{})
err = cc.Put(ctx, testutil.PickKey(int64(i)), fmt.Sprint(i), config.PutOptions{})
assert.NoError(t, err, "error on put")
}

Expand Down Expand Up @@ -249,7 +249,7 @@ func TestCompactHashCheckDetectCorruption(t *testing.T) {

cc := epc.Etcdctl()
for i := 0; i < 10; i++ {
err := cc.Put(ctx, testutil.PickKey(int64(i)), fmt.Sprint(i), config.PutOptions{})
err = cc.Put(ctx, testutil.PickKey(int64(i)), fmt.Sprint(i), config.PutOptions{})
assert.NoError(t, err, "error on put")
}
members, err := cc.MemberList(ctx, false)
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestCompactHashCheckDetectCorruptionInterrupt(t *testing.T) {
t.Log("putting 10 values to the identical key...")
cc := epc.Etcdctl()
for i := 0; i < 10; i++ {
err := cc.Put(ctx, "key", fmt.Sprint(i), config.PutOptions{})
err = cc.Put(ctx, "key", fmt.Sprint(i), config.PutOptions{})
require.NoError(t, err, "error on put")
}

Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/ctl_v3_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ func TestRestoreCompactionRevBump(t *testing.T) {
t.Log("Ensuring the restored member has the correct data...")
hasKVs(t, ctl, kvs, currentRev, baseRev)
for i := range unsnappedKVs {
v, err := ctl.Get(context.Background(), unsnappedKVs[i].Key, config.GetOptions{})
require.NoError(t, err)
v, gerr := ctl.Get(context.Background(), unsnappedKVs[i].Key, config.GetOptions{})
require.NoError(t, gerr)
require.Equal(t, int64(0), v.Count)
}

Expand Down
Loading
Loading