Skip to content

Commit

Permalink
*: enable noloopclosure (#37153)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei committed Aug 26, 2022
1 parent f358b6a commit 909341b
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 19 deletions.
12 changes: 10 additions & 2 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,14 @@ def go_deps():
sum = "h1:DddqAaWDpywytcG8w/qoQ5sAN8X12d3Z3koB0C3Rxsc=",
version = "v0.0.0-20160511215533-1f3b11f56072",
)
go_repository(
name = "com_github_fatanugraha_noloopclosure",
build_file_proto_mode = "disable",
importpath = "github.com/fatanugraha/noloopclosure",
sum = "h1:AhepjAikNpk50qTZoipHZqeZtnyKT/C2Tk5dGn7nC+A=",
version = "v0.1.1",
)

go_repository(
name = "com_github_fatih_color",
build_file_proto_mode = "disable_global",
Expand Down Expand Up @@ -1572,8 +1580,8 @@ def go_deps():
name = "com_github_gostaticanalysis_testutil",
build_file_proto_mode = "disable",
importpath = "github.com/gostaticanalysis/testutil",
sum = "h1:d2/eIbH9XjD1fFwD5SHv8x168fjbQ9PB8hvs8DSEC08=",
version = "v0.3.1-0.20210208050101-bfb5c8eec0e4",
sum = "h1:nhdCmubdmDF6VEatUNjgUZBJKWRqugoISdUv3PPQgHY=",
version = "v0.4.0",
)

go_repository(
Expand Down
1 change: 1 addition & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ nogo(
"//build/linter/ineffassign:ineffassign",
"//build/linter/makezero:makezero",
"//build/linter/misspell:misspell",
"//build/linter/noloopclosure:noloopclosure",
"//build/linter/prealloc:prealloc",
"//build/linter/predeclared:predeclared",
"//build/linter/unconvert:unconvert",
Expand Down
12 changes: 12 additions & 0 deletions build/linter/noloopclosure/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "noloopclosure",
srcs = ["analysis.go"],
importpath = "github.com/pingcap/tidb/build/linter/noloopclosure",
visibility = ["//visibility:public"],
deps = [
"//build/linter/util",
"@com_github_fatanugraha_noloopclosure//:go_default_library",
],
)
27 changes: 27 additions & 0 deletions build/linter/noloopclosure/analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package noloopclosure

import (
nlc "github.com/fatanugraha/noloopclosure"
"github.com/pingcap/tidb/build/linter/util"
)

// Analyzer is the analyzer struct of misspell.
var Analyzer = nlc.Analyzer

func init() {
util.SkipAnalyzer(Analyzer)
}
12 changes: 12 additions & 0 deletions build/nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@
".*_generated\\.go$": "ignore generated code"
}
},
"noloopclosure": {
"exclude_files": {
"/external/": "no need to vet third party code",
".*_generated\\.go$": "ignore generated code"
},
"only_files": {
"kv/": "kv code",
"util/memory": "util/memory",
"ddl/": "ddl",
"planner/": "planner"
}
},
"pkgfact": {
"exclude_files": {
"/external/": "no need to vet third party code",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/dgraph-io/ristretto v0.1.1-0.20220403145359-8e850b710d6d
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13
github.com/docker/go-units v0.4.0
github.com/fatanugraha/noloopclosure v0.1.1
github.com/fatih/color v1.13.0
github.com/fsouza/fake-gcs-server v1.19.0
github.com/go-sql-driver/mysql v1.6.0
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ github.com/etcd-io/gofail v0.0.0-20190801230047-ad7f989257ca/go.mod h1:49H/RkXP8
github.com/evanphx/json-patch v4.1.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a/go.mod h1:7Ga40egUymuWXxAe151lTNnCv97MddSOVsjpPPkityA=
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
github.com/fatanugraha/noloopclosure v0.1.1 h1:AhepjAikNpk50qTZoipHZqeZtnyKT/C2Tk5dGn7nC+A=
github.com/fatanugraha/noloopclosure v0.1.1/go.mod h1:Mi9CiG5QvEgvPLtZLsTzjYwjIDnWAbo10r0BG7JpJII=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
Expand Down Expand Up @@ -476,8 +478,8 @@ github.com/gostaticanalysis/comment v1.4.2 h1:hlnx5+S2fY9Zo9ePo4AhgYsYHbM2+eAv8m
github.com/gostaticanalysis/comment v1.4.2/go.mod h1:KLUTGDv6HOCotCH8h2erHKmpci2ZoR8VPu34YA2uzdM=
github.com/gostaticanalysis/forcetypeassert v0.1.0 h1:6eUflI3DiGusXGK6X7cCcIgVCpZ2CiZ1Q7jl6ZxNV70=
github.com/gostaticanalysis/forcetypeassert v0.1.0/go.mod h1:qZEedyP/sY1lTGV1uJ3VhWZ2mqag3IkWsDHVbplHXak=
github.com/gostaticanalysis/testutil v0.3.1-0.20210208050101-bfb5c8eec0e4 h1:d2/eIbH9XjD1fFwD5SHv8x168fjbQ9PB8hvs8DSEC08=
github.com/gostaticanalysis/testutil v0.3.1-0.20210208050101-bfb5c8eec0e4/go.mod h1:D+FIZ+7OahH3ePw/izIEeH5I06eKs1IKI4Xr64/Am3M=
github.com/gostaticanalysis/testutil v0.4.0 h1:nhdCmubdmDF6VEatUNjgUZBJKWRqugoISdUv3PPQgHY=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y=
Expand Down
1 change: 1 addition & 0 deletions kv/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func RunInNewTxn(ctx context.Context, store Storage, retryable bool, f func(ctx
if v := val.(string); len(v) > 0 {
switch v {
case "retry_once":
//nolint:noloopclosure
if i == 0 {
err = ErrTxnRetryable
}
Expand Down
23 changes: 13 additions & 10 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5469,19 +5469,22 @@ func (b *PlanBuilder) buildUpdateLists(ctx context.Context, tableList []*ast.Tab
dependentColumnsModified[col.UniqueID] = true
} else {
// rewrite with generation expression
rewritePreprocess := func(expr ast.Node) ast.Node {
switch x := expr.(type) {
case *ast.ColumnName:
return &ast.ColumnName{
Schema: assign.Column.Schema,
Table: assign.Column.Table,
Name: x.Name,
rewritePreprocess := func(assign *ast.Assignment) func(expr ast.Node) ast.Node {
return func(expr ast.Node) ast.Node {
switch x := expr.(type) {
case *ast.ColumnName:
return &ast.ColumnName{
Schema: assign.Column.Schema,
Table: assign.Column.Table,
Name: x.Name,
}
default:
return expr
}
default:
return expr
}
}
newExpr, np, err = b.rewriteWithPreprocess(ctx, assign.Expr, p, nil, nil, false, rewritePreprocess)

newExpr, np, err = b.rewriteWithPreprocess(ctx, assign.Expr, p, nil, nil, false, rewritePreprocess(assign))
if err != nil {
return nil, nil, false, err
}
Expand Down
9 changes: 6 additions & 3 deletions testkit/testfork/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,13 @@ type T struct {
// RunTest runs the test function `f` multiple times util all the values in `Pick` are tested.
func RunTest(t *testing.T, f func(t *T)) {
idx := 0
for stack := newPickStack(); stack.Valid(); stack.NextStack() {
success := t.Run("", func(t *testing.T) {
runFunc := func(stack *pickStack, f func(t *T)) func(t *testing.T) {
return func(t *testing.T) {
f(&T{T: t, stack: stack})
})
}
}
for stack := newPickStack(); stack.Valid(); stack.NextStack() {
success := t.Run("", runFunc(stack, f))

if !success {
_, err := fmt.Fprintf(os.Stderr, "SubTest #%v failed, failed values: %s\n", idx, stack.ValuesText())
Expand Down
9 changes: 6 additions & 3 deletions util/memory/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,12 @@ func (t *Tracker) Release(bytes int64) {
if tracker.shouldRecordRelease() {
// use fake ref instead of obj ref, otherwise obj will be reachable again and gc in next cycle
newRef := &finalizerRef{}
runtime.SetFinalizer(newRef, func(ref *finalizerRef) {
tracker.release(bytes)
})
finalizer := func(tracker *Tracker) func(ref *finalizerRef) {
return func(ref *finalizerRef) {
tracker.release(bytes)
}
}
runtime.SetFinalizer(newRef, finalizer(tracker))
tracker.recordRelease(bytes)
return
}
Expand Down

0 comments on commit 909341b

Please sign in to comment.