Skip to content

Commit

Permalink
Merge ed05d68 into 49d0969
Browse files Browse the repository at this point in the history
  • Loading branch information
nkcr committed Apr 8, 2020
2 parents 49d0969 + ed05d68 commit 68d75b3
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
language: go

go:
- 1.13
- 1.14

jobs:
include:
- stage: "Lint"
script:
- go mod tidy && [ -z "$(git status -s)" ]
- make lint
- make vet

- stage: "Test"
name: "Unit Tests"
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ lint:
@go get -v honnef.co/go/tools/cmd/staticcheck
@go mod tidy
staticcheck ./...

vet:
@echo "⚠️ Warning: the following only works with go >= 1.14" && \
go install ./internal/mcheck && \
go vet -vettool=`go env GOPATH`/bin/mcheck -commentLen -ifInit ./...
3 changes: 2 additions & 1 deletion consensus/qsc/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ func (b *bTLCB) merge(prepare, commit *View) (*View, error) {
ret.Received[node] = msg
// The broadcasted set is filled and later on cleaned according to
// the spread threshold.
if _, ok := counter[node]; !ok {
_, found := counter[node]
if !found {
counter[node] = 0
ret.Broadcasted[node] = msg
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
go.dedis.ch/kyber/v3 v3.0.12
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
google.golang.org/grpc v1.27.1
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74 h1:4cFkmztxtMslUX2SctSl+blCyXfpzhGOy9LhKAqSMA4=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d h1:/iIZNFGxc/a7C3yWjGcnboV+Tkc7mxr+p6fDztwoxuM=
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
103 changes: 103 additions & 0 deletions internal/mcheck/mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

// This package provides a custom checks for "go vet".
// It can be used like the following:
// `go build && go vet -vettool=./check -commentLen -ifInit ./...`

import (
"go/ast"
"strings"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/analysis/unitchecker"
)

// MaxLen is the maximum length of a comment
var MaxLen = 80

// This check verifies that no comments exceed the "MaxLen" length. It ignores
// files that have as first comment a "// Code genereated..." comment and it
// ignores comments that start with "//go:generate"
var commentLenAnalyzer = &analysis.Analyzer{
Name: "commentLen",
Doc: "checks the lengths of comments",
Run: runComment,
}

// This check ensures that no if has an initialization statement
var ifInitAnalyzer = &analysis.Analyzer{
Name: "ifInit",
Doc: "checks that no if with an initialization statement are used",
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
Run: runIfInitCheck,
}

func main() {
unitchecker.Main(
commentLenAnalyzer,
ifInitAnalyzer,
)
}

// run parses all the comments in ast.File
func runComment(pass *analysis.Pass) (interface{}, error) {
fileLoop:
for _, file := range pass.Files {
isFirst := true
for _, cg := range file.Comments {
for _, c := range cg.List {
if isFirst && strings.HasPrefix(c.Text, "// Code generated") {
continue fileLoop
}
// in case of /* */ comment there might be multiple lines
lines := strings.Split(c.Text, "\n")
for _, line := range lines {
if strings.HasPrefix(line, "//go:generate") {
continue
}
if len(line) > MaxLen {
pass.Reportf(c.Pos(), "Comment too long: %s (%d)",
line, len(line))
}
}
isFirst = false
}
}
}

return nil, nil
}

// runIfInitCheck parses all the if statement and checks if there is an
// initialization statement used.
func runIfInitCheck(pass *analysis.Pass) (interface{}, error) {
fileLoop:
for _, file := range pass.Files {
// We ignore generated files
if len(file.Comments) != 0 {
cg := file.Comments[0]
if len(cg.List) != 0 {
comment := cg.List[0]
if strings.HasPrefix(comment.Text, "// Code generated") {
continue fileLoop
}
}
}

ast.Inspect(file, func(node ast.Node) bool {
switch x := node.(type) {
case *ast.IfStmt:
if x.Init != nil {
pass.Reportf(x.Pos(), "Please do not do initialization "+
"in if statement")
}
}
return true
})
}

return nil, nil
}
15 changes: 15 additions & 0 deletions internal/mcheck/mod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"testing"

"golang.org/x/tools/go/analysis/analysistest"
)

func TestCommentLen(t *testing.T) {
analysistest.Run(t, analysistest.TestData(), commentLenAnalyzer, "comment")
}

func TestIfCheck(t *testing.T) {
analysistest.Run(t, analysistest.TestData(), ifInitAnalyzer, "ifcheck")
}
13 changes: 13 additions & 0 deletions internal/mcheck/testdata/src/comment/mod1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package comment

// This line should be ok, this is why there is no "want" at the end of it,
// which is the way of testing an analyzer.

// This line is too long and should raise an error because it exceed the 80 chars limit // want ""

/*
This line is not too long and should raise an error because it exceed the 80
chars
*/

//go:generate this line should be ignore even if it's too long because it started with a go:generate
5 changes: 5 additions & 0 deletions internal/mcheck/testdata/src/comment/mod2.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions internal/mcheck/testdata/src/ifcheck/mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ifcheck

func dummy() {
// Should be good
a := true
if a {

}

// Should raise a concern
if b := false; b { // want "Please do not do initialization in if statement"

}

// Should work if nested
for i := 0; i < 1; i++ {
// No concern
if i == 2 {

}
// Should raise a concern
if c := true; c { // want "Please do not do initialization in if statement"

}
}
}
Empty file.
4 changes: 2 additions & 2 deletions ledger/inventory/mem/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func (page inMemoryPage) GetIndex() uint64 {
return page.index
}

// GetFootprint implements inventory.Page. It returns the integrity footprint of the
// page.
// GetFootprint implements inventory.Page. It returns the integrity footprint of
// the page.
func (page inMemoryPage) GetFootprint() []byte {
return page.footprint[:]
}
Expand Down
3 changes: 2 additions & 1 deletion mino/minoch/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func (m *Manager) insert(inst *Minoch) error {
m.Lock()
defer m.Unlock()

if _, ok := m.instances[string(text)]; ok {
_, found := m.instances[string(text)]
if found {
return xerrors.New("identifier already exists")
}

Expand Down
4 changes: 2 additions & 2 deletions mino/minogrpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,8 +1072,8 @@ func TestSender_Send(t *testing.T) {
t.Error("unexpected error from send: ", err)
}

// giving an empty address should add an error since it won't be found in the list of
// participants
// giving an empty address should add an error since it won't be found in
// the list of participants
addr := address{}
errs = sender.Send(&empty.Empty{}, addr)
err, more = <-errs
Expand Down

0 comments on commit 68d75b3

Please sign in to comment.