forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
forbiddenmethod: lint against
(*errgroup.Group).Go
and go
This helps move the needle on cockroachdb#58164 by introducing linters that force both the use of a `ctxgroup` over an `errgroup` and prevent direct use of the `go` keyword. It's part of `make lint`, but can be invoked stand-alone as well: ``` go vet -vettool ./bin/roachvet -errgroupgo ./pkg/somewhere go vet -vettool ./bin/roachvet -nakedgo ./pkg/somewhere ``` Release note: None
- Loading branch information
Showing
8 changed files
with
233 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2019 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package forbiddenmethod_test | ||
|
||
import "github.com/cockroachdb/cockroach/pkg/build/bazel" | ||
|
||
func init() { | ||
if bazel.BuiltWithBazel() { | ||
bazel.SetGoEnv() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package forbiddenmethod | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"strings" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/testutils/lint/passes/passesutil" | ||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
"golang.org/x/tools/go/ast/inspector" | ||
) | ||
|
||
const nakedGoPassName = "nakedgo" | ||
|
||
// NakedGoAnalyzer prevents use of the `go` keyword. | ||
var NakedGoAnalyzer = &analysis.Analyzer{ | ||
Name: nakedGoPassName, | ||
Doc: "Prevents direct use of the 'go' keyword. Goroutines should be launched through Stopper.", | ||
Requires: []*analysis.Analyzer{inspect.Analyzer}, | ||
Run: func(pass *analysis.Pass) (interface{}, error) { | ||
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
inspect.Preorder([]ast.Node{ | ||
(*ast.GoStmt)(nil), | ||
}, func(n ast.Node) { | ||
node := n.(*ast.GoStmt) | ||
|
||
const debug = false | ||
|
||
// NB: we're not using passesutil.HasNoLintComment because it | ||
// has false negatives (i.e. comments apply to infractions that | ||
// they were clearly not intended for). | ||
// | ||
// NB: we could also use the approach `analysistest` uses, it's | ||
// using the line of the comment instead of something positional. | ||
// This means though that the comment isn't associated to GoStmt | ||
// itself, but to one of its children. | ||
f := passesutil.FindContainingFile(pass, n) | ||
cm := ast.NewCommentMap(pass.Fset, node, f.Comments) | ||
var cc *ast.Comment | ||
for _, cg := range cm[n] { | ||
for _, c := range cg.List { | ||
if c.Pos() < node.Go { | ||
// The current comment is "before" the `go` invocation, so it's | ||
// not relevant for silencing the lint. | ||
continue | ||
} | ||
if cc == nil || cc.Pos() > node.Go { | ||
// This comment is after, but closer to the `go` invocation than | ||
// previous candidate. | ||
cc = c | ||
if debug { | ||
fmt.Printf("closest comment now %d-%d: %s\n", cc.Pos(), cc.End(), cc.Text) | ||
} | ||
} | ||
} | ||
} | ||
if cc != nil && strings.Contains(cc.Text, "nolint:"+nakedGoPassName) { | ||
if debug { | ||
fmt.Printf("GoStmt at: %d-%d\n", n.Pos(), n.End()) | ||
fmt.Printf("GoStmt.Go at: %d\n", node.Go) | ||
fmt.Printf("GoStmt.Call at: %d-%d\n", node.Call.Pos(), node.Call.End()) | ||
} | ||
|
||
goPos := pass.Fset.Position(node.End()) | ||
cmPos := pass.Fset.Position(cc.Pos()) | ||
|
||
if goPos.Line == cmPos.Line { | ||
if debug { | ||
fmt.Printf("suppressing lint because of %d-%d: %s\n", cc.Pos(), cc.End(), cc.Text) | ||
} | ||
return | ||
} | ||
} | ||
|
||
pass.Report(analysis.Diagnostic{ | ||
Pos: n.Pos(), | ||
Message: "Use of go keyword not allowed, use a Stopper instead", | ||
}) | ||
}) | ||
return nil, nil | ||
}, | ||
} |
25 changes: 25 additions & 0 deletions
25
pkg/testutils/lint/passes/forbiddenmethod/naked_go_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2019 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package forbiddenmethod_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/testutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/lint/passes/forbiddenmethod" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestNakedGo(t *testing.T) { | ||
testdata := testutils.TestDataPath(t) | ||
analysistest.TestData = func() string { return testdata } | ||
analysistest.Run(t, testdata, forbiddenmethod.NakedGoAnalyzer, "nakedgotest") | ||
} |
48 changes: 48 additions & 0 deletions
48
pkg/testutils/lint/passes/forbiddenmethod/testdata/src/nakedgotest/foo.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package nakedgotest | ||
|
||
func toot() { | ||
// The nolint comment below should have no effect. | ||
// For some reason though it shows up in the CommentMap | ||
// for the *ast.GoStmt, though. No idea why. | ||
|
||
//nolint:nakedgo should not help anyone | ||
} | ||
|
||
func A() { | ||
//nolint: I'm a noop | ||
go func() {}() // want `Use of go keyword not allowed, use a Stopper instead` | ||
go toot() // want `Use of go keyword not allowed, use a Stopper instead` | ||
go /* nolint: nakedgo not helping */ toot() // want `Use of go keyword not allowed, use a Stopper instead` | ||
/* nolint: nakedgo nope */ go toot() // want `Use of go keyword not allowed, use a Stopper instead` | ||
//nolint:nakedgo nope, this one neither | ||
|
||
go func() {}() // want `Use of go keyword not allowed, use a Stopper instead` | ||
|
||
go func() {}() //nolint:nakedgo | ||
|
||
go toot() //nolint:nakedgo | ||
|
||
go func() { /* want `Use of go keyword not allowed, use a Stopper instead` */ //nolint:nakedgo | ||
_ = 0 | ||
}() | ||
|
||
go func() { // want `Use of go keyword not allowed, use a Stopper instead` | ||
_ = 0 //nolint:nakedgo | ||
}() | ||
|
||
// Finally, doing it right! | ||
|
||
go func() { | ||
_ = 0 | ||
}() //nolint:nakedgo | ||
} |