Skip to content

Commit

Permalink
Enforce the use of a blank identifier for interface compliance (ava-l…
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored May 12, 2023
1 parent f0a86cc commit e2b4d9a
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 18 deletions.
13 changes: 12 additions & 1 deletion scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fi
# by default, "./scripts/lint.sh" runs all lint tests
# to run only "license_header" test
# TESTS='license_header' ./scripts/lint.sh
TESTS=${TESTS:-"golangci_lint license_header require_error_is_no_funcs_as_params single_import"}
TESTS=${TESTS:-"golangci_lint license_header require_error_is_no_funcs_as_params single_import interface_compliance_nil"}

function test_golangci_lint {
go install -v github.com/golangci/golangci-lint/cmd/[email protected]
Expand Down Expand Up @@ -57,6 +57,17 @@ function test_require_error_is_no_funcs_as_params {
fi
}

# Ref: https://go.dev/doc/effective_go#blank_implements
function test_interface_compliance_nil {
if grep -R -o -P '_ .+? = &.+?\{\}' .; then
echo ""
echo "Interface compliance checks need to be of the form:"
echo " var _ json.Marshaler = (*RawMessage)(nil)"
echo ""
return 1
fi
}

function run {
local test="${1}"
shift 1
Expand Down
2 changes: 1 addition & 1 deletion vms/avm/txs/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
)

var (
_ Mempool = &mempool{}
_ Mempool = (*mempool)(nil)

errDuplicateTx = errors.New("duplicate tx")
errTxTooLarge = errors.New("tx too large")
Expand Down
2 changes: 1 addition & 1 deletion vms/rpcchainvm/gruntime/runtime_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
pb "github.com/ava-labs/avalanchego/proto/pb/vm/runtime"
)

var _ pb.RuntimeServer = &Server{}
var _ pb.RuntimeServer = (*Server)(nil)

// Server is a VM runtime initializer controlled by RPC.
type Server struct {
Expand Down
2 changes: 1 addition & 1 deletion x/merkledb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package merkledb

import "github.com/ava-labs/avalanchego/database"

var _ database.Batch = &batch{}
var _ database.Batch = (*batch)(nil)

// batch is a write-only database that commits changes to its host database
// when Write is called.
Expand Down
4 changes: 2 additions & 2 deletions x/merkledb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const (
)

var (
_ TrieView = &Database{}
_ database.Database = &Database{}
_ TrieView = (*Database)(nil)
_ database.Database = (*Database)(nil)

Codec, Version = newCodec()

Expand Down
2 changes: 1 addition & 1 deletion x/merkledb/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package merkledb

import "github.com/ava-labs/avalanchego/database"

var _ database.Iterator = &iterator{}
var _ database.Iterator = (*iterator)(nil)

type iterator struct {
db *Database
Expand Down
4 changes: 2 additions & 2 deletions x/merkledb/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

var (
_ merkleMetrics = &mockMetrics{}
_ merkleMetrics = &metrics{}
_ merkleMetrics = (*mockMetrics)(nil)
_ merkleMetrics = (*metrics)(nil)
)

type merkleMetrics interface {
Expand Down
4 changes: 2 additions & 2 deletions x/merkledb/trieview.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
const defaultPreallocationSize = 100

var (
_ TrieView = (*trieView)(nil)

ErrCommitted = errors.New("view has been committed")
ErrInvalid = errors.New("the trie this view was based on has changed, rendering this view invalid")
ErrOddLengthWithValue = errors.New(
Expand All @@ -36,8 +38,6 @@ var (
ErrViewIsNotAChild = errors.New("passed in view is required to be a child of the current view")
ErrNoValidRoot = errors.New("a valid root was not provided to the trieView constructor")

_ TrieView = &trieView{}

numCPU = runtime.NumCPU()
)

Expand Down
2 changes: 1 addition & 1 deletion x/sync/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

var (
_ Client = &client{}
_ Client = (*client)(nil)

errInvalidRangeProof = errors.New("failed to verify range proof")
errTooManyKeys = errors.New("response contains more than requested keys")
Expand Down
4 changes: 2 additions & 2 deletions x/sync/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

var (
_ SyncMetrics = &mockMetrics{}
_ SyncMetrics = &metrics{}
_ SyncMetrics = (*mockMetrics)(nil)
_ SyncMetrics = (*metrics)(nil)
)

type SyncMetrics interface {
Expand Down
2 changes: 1 addition & 1 deletion x/sync/network_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
const minRequestHandlingDuration = 100 * time.Millisecond

var (
_ NetworkClient = &networkClient{}
_ NetworkClient = (*networkClient)(nil)

ErrAcquiringSemaphore = errors.New("error acquiring semaphore")
ErrRequestFailed = errors.New("request failed")
Expand Down
2 changes: 1 addition & 1 deletion x/sync/response_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package sync
// Look into making a struct to handle requests/responses that uses a sync pool
// to avoid allocations.

var _ ResponseHandler = &responseHandler{}
var _ ResponseHandler = (*responseHandler)(nil)

// Handles responses/failure notifications for a sent request.
// Exactly one of OnResponse or OnFailure is eventually called.
Expand Down
2 changes: 1 addition & 1 deletion x/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
syncpb "github.com/ava-labs/avalanchego/proto/pb/sync"
)

var _ Client = &mockClient{}
var _ Client = (*mockClient)(nil)

func newNoopTracer() trace.Tracer {
tracer, _ := trace.New(trace.Config{Enabled: false})
Expand Down
2 changes: 1 addition & 1 deletion x/sync/syncworkheap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/google/btree"
)

var _ heap.Interface = &syncWorkHeap{}
var _ heap.Interface = (*syncWorkHeap)(nil)

type heapItem struct {
workItem *syncWorkItem
Expand Down

0 comments on commit e2b4d9a

Please sign in to comment.