Skip to content

Commit

Permalink
*: enable unconvert (#35821)
Browse files Browse the repository at this point in the history
ref #35345
  • Loading branch information
hawkingrei authored Jun 29, 2022
1 parent 5eac3b6 commit 47a4f3a
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 1 deletion.
7 changes: 7 additions & 0 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,13 @@ def go_deps():
sum = "h1:leSNB7iYzLYSSx3J/s5sVf4Drkc68W2wm4Ixh/mr0us=",
version = "v0.0.0-20180630174525-215b22d4de21",
)
go_repository(
name = "com_github_golangci_unconvert",
build_file_proto_mode = "disable",
importpath = "github.com/golangci/unconvert",
sum = "h1:zwtduBRr5SSWhqsYNgcuWO2kFlpdOZbP0+yRjmvPGys=",
version = "v0.0.0-20180507085042-28b1c447d1f4",
)

go_repository(
name = "com_github_gomodule_redigo",
Expand Down
1 change: 0 additions & 1 deletion br/pkg/lightning/mydump/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ go_library(
"//util/slice",
"//util/table-filter",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_xitongsys_parquet_go//parquet",
"@com_github_xitongsys_parquet_go//reader",
"@com_github_xitongsys_parquet_go//source",
Expand Down
1 change: 1 addition & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ nogo(
"//build/linter/gofmt:gofmt",
"//build/linter/ineffassign:ineffassign",
"//build/linter/prealloc:prealloc",
"//build/linter/unconvert:unconvert",
] + staticcheck_analyzers(STATICHECK_ANALYZERS),
)
14 changes: 14 additions & 0 deletions build/linter/unconvert/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "unconvert",
srcs = ["analysis.go"],
importpath = "github.com/pingcap/tidb/build/linter/unconvert",
visibility = ["//visibility:public"],
deps = [
"//build/linter/util",
"@com_github_golangci_unconvert//:unconvert",
"@org_golang_x_tools//go/analysis",
"@org_golang_x_tools//go/loader",
],
)
61 changes: 61 additions & 0 deletions build/linter/unconvert/analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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 unconvert

import (
"fmt"
"go/token"
"go/types"

unconvertAPI "github.com/golangci/unconvert"
"github.com/pingcap/tidb/build/linter/util"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/loader"
)

// Name is the name of the analyzer.
const Name = "unconvert"

// Analyzer is the analyzer struct of unconvert.
var Analyzer = &analysis.Analyzer{
Name: Name,
Doc: "Remove unnecessary type conversions",
Run: run,
}

func run(pass *analysis.Pass) (interface{}, error) {
var createdPkgs []*loader.PackageInfo
createdPkgs = append(createdPkgs, util.MakeFakeLoaderPackageInfo(pass))
allPkgs := map[*types.Package]*loader.PackageInfo{}
for _, pkg := range createdPkgs {
pkg := pkg
allPkgs[pkg.Pkg] = pkg
}
prog := &loader.Program{
Fset: pass.Fset,
Imported: nil, // not used without .Created in any linter
Created: createdPkgs, // all initial packages
AllPackages: allPkgs, // all initial packages and their depndencies
}
positions := unconvertAPI.Run(prog)
if len(positions) == 0 {
return nil, nil
}

for _, pos := range positions {
pass.Reportf(token.Pos(pos.Offset), fmt.Sprintf("[%s] Unnecessary conversion", Name))
}
return nil, nil
}
1 change: 1 addition & 0 deletions build/linter/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ go_library(
deps = [
"@co_honnef_go_tools//analysis/report",
"@org_golang_x_tools//go/analysis",
"@org_golang_x_tools//go/loader",
],
)
20 changes: 20 additions & 0 deletions build/linter/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/loader"
"honnef.co/go/tools/analysis/report"
)

Expand Down Expand Up @@ -147,3 +148,22 @@ func FormatCode(code string) string {

return fmt.Sprintf("`%s`", code)
}

// MakeFakeLoaderPackageInfo creates a fake loader.PackageInfo for a given package.
func MakeFakeLoaderPackageInfo(pass *analysis.Pass) *loader.PackageInfo {
var errs []error

typeInfo := pass.TypesInfo

return &loader.PackageInfo{
Pkg: pass.Pkg,
Importable: true, // not used
TransitivelyErrorFree: true, // not used

// use compiled (preprocessed) go files AST;
// AST linters use not preprocessed go files AST
Files: pass.Files,
Errors: errs,
Info: *typeInfo,
}
}
9 changes: 9 additions & 0 deletions build/nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@
".*_generated\\.go$": "ignore generated code"
}
},
"unconvert": {
"exclude_files": {
"/external/": "no need to vet third party code",
".*\\.pb\\.go$": "generated code",
"parser/parser.go": "generated code",
"/cgo/": "no need to vet third party code for cgo",
".*_generated\\.go$": "ignore generated code"
}
},
"unmarshal": {
"exclude_files": {
"/external/": "no need to vet third party code",
Expand Down
1 change: 1 addition & 0 deletions executor/seqtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_test(
"//kv",
"//meta/autoid",
"//metrics",
"//parser/ast",
"//parser/model",
"//parser/mysql",
"//parser/terror",
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,14 @@ require (
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1581
github.com/charithe/durationcheck v0.0.9
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
github.com/gordonklaus/ineffassign v0.0.0-20210914165742-4cc7213b9bc8
github.com/kyoh86/exportloopref v0.1.8
honnef.co/go/tools v0.0.1-2020.1.4
)

require github.com/kisielk/gotool v1.0.0 // indirect

require (
cloud.google.com/go v0.100.2 // indirect
cloud.google.com/go/compute v1.2.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a h1:iR3fYXUjHCR97qWS
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a/go.mod h1:9qCChq59u/eW8im404Q2WWTrnBUQKjpNYKMbU4M7EFU=
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21 h1:leSNB7iYzLYSSx3J/s5sVf4Drkc68W2wm4Ixh/mr0us=
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21/go.mod h1:tf5+bzsHdTM0bsB7+8mt0GUMvjCgwLpTapNZHU8AajI=
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 h1:zwtduBRr5SSWhqsYNgcuWO2kFlpdOZbP0+yRjmvPGys=
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4/go.mod h1:Izgrg8RkN3rCIMLGE9CyYmU9pY2Jer6DgANEnZ/L/cQ=
github.com/gomodule/redigo v1.7.1-0.20190724094224-574c33c3df38/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
Expand Down Expand Up @@ -511,6 +513,7 @@ github.com/kataras/pio v0.0.0-20190103105442-ea782b38602d/go.mod h1:NV88laa9UiiD
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0 h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.9.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down
2 changes: 2 additions & 0 deletions sessiontxn/isolation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ go_test(
"//parser",
"//parser/ast",
"//planner",
"//session",
"//sessionctx",
"//sessiontxn",
"//testkit",
"//testkit/testsetup",
"//types",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_pingcap_kvproto//pkg/kvrpcpb",
Expand Down
1 change: 1 addition & 0 deletions testkit/testfork/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ go_test(
name = "testfork_test",
srcs = ["fork_test.go"],
embed = [":testfork"],
deps = ["@com_github_stretchr_testify//require"],
)

0 comments on commit 47a4f3a

Please sign in to comment.