-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces a new linter: `loopvarcapture`. It reports uses of loop variables captured by reference in Go routines or defer statements, a common source of data races [1]. `govet` currently has a similar linter [2]; however, that project prioritizes having no false positives at the expense of allowing false negatives. This linter, on the other hand, represents the opinion that loop variables should not be captured by reference in Go routines even when it's safe to do so. That behavior is confusing and concurrency added to related code over time could lead to the introduction of data races, potentially manifesting as bugs in the product or flakiness in the tests. These issues are hard to debug and take a lot of developer time. Developers are still able to use their own judgement and disable this linter in specific instances by using a `nolint` comment. [1] A Study of Real-World Data Races in Golang: https://arxiv.org/pdf/2204.00764.pdf [2] https://github.com/golangci/govet/blob/44ddbe260190d79165f4150b828650780405d801/rangeloop.go#L36 Resolves: #80803. Release note: None.
- Loading branch information
1 parent
b0bb1e2
commit b3d21b2
Showing
12 changed files
with
967 additions
and
4 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,33 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "loopvarcapture", | ||
srcs = [ | ||
"loop.go", | ||
"loopvarcapture.go", | ||
], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/testutils/lint/passes/loopvarcapture", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/testutils/lint/passes/passesutil", | ||
"@org_golang_x_tools//go/analysis", | ||
"@org_golang_x_tools//go/analysis/passes/inspect", | ||
"@org_golang_x_tools//go/ast/inspector", | ||
"@org_golang_x_tools//go/types/typeutil", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "loopvarcapture_test", | ||
srcs = ["loopvarcapture_test.go"], | ||
data = glob(["testdata/**"]) + [ | ||
"@go_sdk//:files", | ||
], | ||
deps = [ | ||
":loopvarcapture", | ||
"//pkg/build/bazel", | ||
"//pkg/testutils", | ||
"//pkg/testutils/skip", | ||
"@org_golang_x_tools//go/analysis/analysistest", | ||
], | ||
) |
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,74 @@ | ||
// Copyright 2022 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 loopvarcapture | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
) | ||
|
||
// Loop abstracts away the type of loop (`for` loop with index | ||
// variable vs `range` loops) | ||
type Loop struct { | ||
Vars []*ast.Ident | ||
Body *ast.BlockStmt | ||
} | ||
|
||
// NewLoop creates a new Loop struct according to the node passed. If | ||
// the node does not represent either a `for` loop or a `range` loop, | ||
// this function will panic. | ||
func NewLoop(n ast.Node) *Loop { | ||
switch node := n.(type) { | ||
case *ast.ForStmt: | ||
return newForLoop(node) | ||
case *ast.RangeStmt: | ||
return newRange(node) | ||
default: | ||
panic(fmt.Errorf("unexpected loop node: %#v", n)) | ||
} | ||
} | ||
|
||
// IsEmpty returns whether the loop is empty for the purposes of this | ||
// linter; in other words, whether there no loop variables, or whether | ||
// the loop has zero statements. | ||
func (l *Loop) IsEmpty() bool { | ||
return len(l.Vars) == 0 || len(l.Body.List) == 0 | ||
} | ||
|
||
func newForLoop(stmt *ast.ForStmt) *Loop { | ||
loop := Loop{Body: stmt.Body} | ||
|
||
switch post := stmt.Post.(type) { | ||
case *ast.AssignStmt: | ||
for _, lhs := range post.Lhs { | ||
loop.addVar(lhs) | ||
} | ||
|
||
case *ast.IncDecStmt: | ||
loop.addVar(post.X) | ||
} | ||
|
||
return &loop | ||
} | ||
|
||
func newRange(stmt *ast.RangeStmt) *Loop { | ||
loop := Loop{Body: stmt.Body} | ||
loop.addVar(stmt.Key) | ||
loop.addVar(stmt.Value) | ||
|
||
return &loop | ||
} | ||
|
||
func (l *Loop) addVar(e ast.Expr) { | ||
if ident, ok := e.(*ast.Ident); ok && ident.Obj != nil { | ||
l.Vars = append(l.Vars, ident) | ||
} | ||
} |
Oops, something went wrong.