From a25773c3a23705a9b37f1a709133036c58b3628c Mon Sep 17 00:00:00 2001 From: Vlad <13818348+walldiss@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:29:44 +0400 Subject: [PATCH] chore(fmt/linters): enable nilerr, nilnil, nolintlint and fix related issues (#3332) Enables nilerr, nilnil, nolintlint checks as well as fixing issues found by those linters --- .golangci.yml | 3 +++ core/eds.go | 4 ++-- core/eds_test.go | 4 ++-- das/daser_test.go | 4 ++-- nodebuilder/fraud/lifecycle.go | 2 +- nodebuilder/node/metrics.go | 3 ++- share/eds/blockstore.go | 2 +- share/p2p/discovery/discovery.go | 2 +- state/core_access.go | 2 +- 9 files changed, 15 insertions(+), 11 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 89ea8d18ed..e9fc3713de 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -29,6 +29,9 @@ linters: - misspell # - maligned - nakedret + - nilerr + - nilnil + - nolintlint - prealloc # - scopelint - deprecated since v1.39. exportloopref will be used instead - exportloopref diff --git a/core/eds.go b/core/eds.go index c5f9ed6075..daab35fe8f 100644 --- a/core/eds.go +++ b/core/eds.go @@ -28,7 +28,7 @@ import ( // nil is returned in place of the eds. func extendBlock(data types.Data, appVersion uint64, options ...nmt.Option) (*rsmt2d.ExtendedDataSquare, error) { if app.IsEmptyBlock(data, appVersion) { - return nil, nil + return share.EmptyExtendedDataSquare(), nil } // Construct the data square from the block's transactions @@ -62,7 +62,7 @@ func storeEDS( store *eds.Store, window pruner.AvailabilityWindow, ) error { - if eds == nil { + if eds.Equals(share.EmptyExtendedDataSquare()) { return nil } diff --git a/core/eds_test.go b/core/eds_test.go index 723cb77ad1..92d78c3064 100644 --- a/core/eds_test.go +++ b/core/eds_test.go @@ -24,7 +24,7 @@ func TestTrulyEmptySquare(t *testing.T) { eds, err := extendBlock(data, appconsts.LatestVersion) require.NoError(t, err) - assert.Nil(t, eds) + require.True(t, eds.Equals(share.EmptyExtendedDataSquare())) } // TestNonZeroSquareSize tests that the DAH hash of a block with no transactions @@ -39,8 +39,8 @@ func TestEmptySquareWithZeroTxs(t *testing.T) { } eds, err := extendBlock(data, appconsts.LatestVersion) - require.Nil(t, eds) require.NoError(t, err) + require.True(t, eds.Equals(share.EmptyExtendedDataSquare())) // force extend the square using an empty block and compare with the min DAH eds, err = app.ExtendBlock(data, appconsts.LatestVersion) diff --git a/das/daser_test.go b/das/daser_test.go index d0b60125a1..911ed355c1 100644 --- a/das/daser_test.go +++ b/das/daser_test.go @@ -432,9 +432,9 @@ func (m getterStub) GetRangeByHeight( *header.ExtendedHeader, uint64, ) ([]*header.ExtendedHeader, error) { - return nil, nil + panic("implement me") } func (m getterStub) Get(context.Context, libhead.Hash) (*header.ExtendedHeader, error) { - return nil, nil + panic("implement me") } diff --git a/nodebuilder/fraud/lifecycle.go b/nodebuilder/fraud/lifecycle.go index 50f4e1035b..43118784da 100644 --- a/nodebuilder/fraud/lifecycle.go +++ b/nodebuilder/fraud/lifecycle.go @@ -69,7 +69,7 @@ func (breaker *ServiceBreaker[S, H]) Stop(ctx context.Context) error { if breaker.ctx.Err() != nil { // short circuit if the service was already stopped - return nil + return nil //nolint:nilerr } breaker.sub.Cancel() diff --git a/nodebuilder/node/metrics.go b/nodebuilder/node/metrics.go index 5df847b916..efcfd6cf50 100644 --- a/nodebuilder/node/metrics.go +++ b/nodebuilder/node/metrics.go @@ -2,6 +2,7 @@ package node import ( "context" + "fmt" "time" logging "github.com/ipfs/go-log/v2" @@ -72,7 +73,7 @@ func WithMetrics(lc fx.Lifecycle) error { clientReg, err := meter.RegisterCallback(callback, nodeStartTS, totalNodeRunTime, buildInfoGauge) if err != nil { - return nil + return fmt.Errorf("failed to register metrics callback: %w", err) } lc.Append( diff --git a/share/eds/blockstore.go b/share/eds/blockstore.go index e44601870e..a1ac8ab8b6 100644 --- a/share/eds/blockstore.go +++ b/share/eds/blockstore.go @@ -47,7 +47,7 @@ func (bs *blockstore) Has(ctx context.Context, cid cid.Cid) (bool, error) { // key wasn't found in top level blockstore, but could be in datastore while being reconstructed dsHas, dsErr := bs.ds.Has(ctx, dshelp.MultihashToDsKey(cid.Hash())) if dsErr != nil { - return false, nil + return false, nil //nolint:nilerr // return false if error } return dsHas, nil } diff --git a/share/p2p/discovery/discovery.go b/share/p2p/discovery/discovery.go index c5cbd88b68..f2ca04bbbe 100644 --- a/share/p2p/discovery/discovery.go +++ b/share/p2p/discovery/discovery.go @@ -294,7 +294,7 @@ func (d *Discovery) discover(ctx context.Context) bool { wg.Go(func() error { if findCtx.Err() != nil { log.Debug("find has been canceled, skip peer") - return nil + return nil //nolint:nilerr } // we don't pass findCtx so that we don't cancel in progress connections diff --git a/state/core_access.go b/state/core_access.go index f19419885d..9493a0501a 100644 --- a/state/core_access.go +++ b/state/core_access.go @@ -610,7 +610,7 @@ func (ca *CoreAccessor) GrantFee( msg, err := feegrant.NewMsgGrantAllowance(allowance, granter, grantee) if err != nil { - return nil, nil + return nil, err } resp, err := signer.SubmitTx(ctx, []sdktypes.Msg{msg}, user.SetGasLimit(gasLim), withFee(fee))