tools: Add code complexity linter, gocyclo

Gocyclo is a code complexity linter. It uses cyclomatic complexity.
Cyclomatic complexity essentially measures the number of different
paths code could go through. (The conditional in a for loop counts
as adding one path) It looks at this on a per-function level. The
idea that this would be enforcing is that if there are too many
different paths code can go through in a function, it needs to be
better split up. (A function with too many code paths is hard to
reason about)

The complexity which we want the linter to start failing on is
configurable. The default is 10. Change the "Cyclo" parameter in
`tools/gometalinter.json` to try other values.
This commit is contained in:
ValarDragon 2018-07-07 18:59:06 -07:00
parent 37640d6c00
commit a768543d92
3 changed files with 19 additions and 2 deletions

View File

@ -70,6 +70,7 @@ FEATURES
* ineffassign
* errcheck
* unparam
* gocyclo
* [tools] Add `make format` command to automate fixing misspell and gofmt errors.
* [server] Default config now creates a profiler at port 6060, and increase p2p send/recv rates
* [tests] Add WaitForNextNBlocksTM helper method

View File

@ -12,6 +12,7 @@ INEFFASSIGN = github.com/gordonklaus/ineffassign
MISSPELL = github.com/client9/misspell/cmd/misspell
ERRCHECK = github.com/kisielk/errcheck
UNPARAM = mvdan.cc/unparam
GOCYCLO = github.com/alecthomas/gocyclo
DEP_CHECK := $(shell command -v dep 2> /dev/null)
GOLINT_CHECK := $(shell command -v golint 2> /dev/null)
@ -21,6 +22,7 @@ INEFFASSIGN_CHECK := $(shell command -v ineffassign 2> /dev/null)
MISSPELL_CHECK := $(shell command -v misspell 2> /dev/null)
ERRCHECK_CHECK := $(shell command -v errcheck 2> /dev/null)
UNPARAM_CHECK := $(shell command -v unparam 2> /dev/null)
GOCYCLO_CHECK := $(shell command -v gocyclo 2> /dev/null)
check_tools:
ifndef DEP_CHECK
@ -63,6 +65,11 @@ ifndef UNPARAM_CHECK
else
@echo "Found unparam in path."
endif
ifndef GOCYCLO_CHECK
@echo "No gocyclo in path. Install with 'make get_tools'."
else
@echo "Found gocyclo in path."
endif
get_tools:
ifdef DEP_CHECK
@ -113,6 +120,12 @@ else
@echo "Installing unparam"
go get -v $(UNPARAM)
endif
ifdef GOYCLO_CHECK
@echo "goyclo is already installed. Run 'make update_tools' to update."
else
@echo "Installing goyclo"
go get -v $(GOCYCLO)
endif
update_tools:
@echo "Updating dep"
@ -131,6 +144,8 @@ update_tools:
go get -u -v $(ERRCHECK)
@echo "Updating unparam"
go get -u -v $(UNPARAM)
@echo "Updating goyclo"
go get -u -v $(GOCYCLO)
# To avoid unintended conflicts with file names, always add to .PHONY
# unless there is a reason not to.

View File

@ -2,7 +2,8 @@
"Linters": {
"vet": "go tool vet -composites=false :PATH:LINE:MESSAGE"
},
"Enable": ["golint", "vet", "ineffassign", "unparam", "unconvert", "misspell"],
"Enable": ["golint", "vet", "ineffassign", "unparam", "unconvert", "misspell", "gocyclo"],
"Deadline": "500s",
"Vendor": true
"Vendor": true,
"Cyclo": 10
}