From a768543d92a1a555338056f354a51af92dee321b Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Sat, 7 Jul 2018 18:59:06 -0700 Subject: [PATCH] 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. --- CHANGELOG.md | 1 + tools/Makefile | 15 +++++++++++++++ tools/gometalinter.json | 5 +++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e63d01c4b..48ddab70f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/tools/Makefile b/tools/Makefile index 195e67aa5..d58f52d1b 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -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. diff --git a/tools/gometalinter.json b/tools/gometalinter.json index a6c74eebb..32ae434c4 100644 --- a/tools/gometalinter.json +++ b/tools/gometalinter.json @@ -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 } \ No newline at end of file