Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: enable deadcode #35700

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,13 @@ def go_deps():
sum = "h1:iR3fYXUjHCR97qWS8ch1y9zPNsgXThGwjKPrYfqMPks=",
version = "v0.0.0-20190930125516-244bba706f1a",
)
go_repository(
name = "com_github_golangci_go_misc",
build_file_proto_mode = "disable",
importpath = "github.com/golangci/go-misc",
sum = "h1:6RGUuS7EGotKx6J5HIP8ZtyMdiDscjMLfRBSPuzVVeo=",
version = "v0.0.0-20220329215616-d24fe342adfe",
)

go_repository(
name = "com_github_golangci_prealloc",
Expand Down Expand Up @@ -2049,6 +2056,28 @@ def go_deps():
sum = "h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=",
version = "v0.0.0-20200410134404-eec4a21b6bb0",
)
go_repository(
name = "com_github_remyoudompheng_go_dbus",
build_file_proto_mode = "disable",
importpath = "github.com/remyoudompheng/go-dbus",
sum = "h1:CvqZS4QYHBRvx7AeFdimd16HCbLlYsvQMcKDACpJW/c=",
version = "v0.0.0-20121104212943-b7232d34b1d5",
)
go_repository(
name = "com_github_remyoudompheng_go_liblzma",
build_file_proto_mode = "disable",
importpath = "github.com/remyoudompheng/go-liblzma",
sum = "h1:J8J/cgLDRuqXJnwIrRDBvtl+LLsdg7De74znW/BRRq4=",
version = "v0.0.0-20190506200333-81bf2d431b96",
)
go_repository(
name = "com_github_remyoudompheng_go_misc",
build_file_proto_mode = "disable",
importpath = "github.com/remyoudompheng/go-misc",
sum = "h1:eTWZyPUnHcuGRDiryS/l2I7FfKjbU3IBx3IjqHPxuKU=",
version = "v0.0.0-20190427085024-2d6ac652a50e",
)

go_repository(
name = "com_github_rivo_uniseg",
build_file_proto_mode = "disable_global",
Expand Down Expand Up @@ -3009,8 +3038,8 @@ def go_deps():
name = "org_golang_x_net",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/net",
sum = "h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=",
version = "v0.0.0-20220127200216-cd36cc0744dd",
sum = "h1:pZB1TWnKi+o4bENlbzAgLrEbY4RMYmUIRobMcSmfeYc=",
version = "v0.0.0-20220325170049-de3da57026de",
)
go_repository(
name = "org_golang_x_oauth2",
Expand Down
1 change: 1 addition & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ nogo(
"@org_golang_x_tools//go/analysis/passes/unreachable:go_default_library",
"@org_golang_x_tools//go/analysis/passes/unsafeptr:go_default_library",
"@org_golang_x_tools//go/analysis/passes/unusedresult:go_default_library",
"//build/linter/deadcode:deadcode",
"//build/linter/durationcheck:durationcheck",
"//build/linter/exportloopref:exportloopref",
"//build/linter/gofmt:gofmt",
Expand Down
14 changes: 14 additions & 0 deletions build/linter/deadcode/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 = "deadcode",
srcs = ["analyzer.go"],
importpath = "github.com/pingcap/tidb/build/linter/deadcode",
visibility = ["//visibility:public"],
deps = [
"//build/linter/util",
"@com_github_golangci_go_misc//deadcode:go_default_library",
"@org_golang_x_tools//go/analysis",
"@org_golang_x_tools//go/loader",
],
)
63 changes: 63 additions & 0 deletions build/linter/deadcode/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 deadcode

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

deadcodeAPI "github.com/golangci/go-misc/deadcode"
"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 = "deadcode"

// Analyzer is the analyzer struct of deadcode.
var Analyzer = &analysis.Analyzer{
Name: Name,
Doc: "Finds unused code",
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
}
issues, err := deadcodeAPI.Run(prog)
if err != nil {
return nil, err
}
for _, i := range issues {
pass.Reportf(token.Pos(i.Pos.Offset), "[%s] %s is unused", Name, util.FormatCode(i.UnusedIdentName))
}
return nil, nil
}

func init() {
util.SkipAnalyzer(Analyzer)
}
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 @@ -74,6 +74,15 @@
".*_generated\\.go$": "ignore generated code"
}
},
"deadcode": {
"exclude_files": {
"/external/": "no need to vet third party code",
".*_generated\\.go$": "ignore generated code",
"/cgo/": "no need to vet third party code for cgo",
"br/pkg/lightning/manual/allocator.go": "ignore code",
"parser/": "ignore code"
}
},
"deepequalerrors": {
"exclude_files": {
"/external/": "no need to vet third party code",
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ require (
go.uber.org/multierr v1.8.0
go.uber.org/zap v1.21.0
golang.org/x/exp v0.0.0-20220426173459-3bcf042a4bf5
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
golang.org/x/net v0.0.0-20220325170049-de3da57026de
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f
Expand All @@ -98,6 +98,7 @@ require (
require (
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1581
github.com/charithe/durationcheck v0.0.9
github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
github.com/gordonklaus/ineffassign v0.0.0-20210914165742-4cc7213b9bc8
github.com/kyoh86/exportloopref v0.1.8
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ github.com/golang/snappy v0.0.2-0.20190904063534-ff6b7dc882cf/go.mod h1:/XxbfmMg
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe h1:6RGUuS7EGotKx6J5HIP8ZtyMdiDscjMLfRBSPuzVVeo=
github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe/go.mod h1:gjqyPShc/m8pEMpk0a3SeagVb0kaqvhscv+i9jI5ZhQ=
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a h1:iR3fYXUjHCR97qWS8ch1y9zPNsgXThGwjKPrYfqMPks=
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=
Expand Down Expand Up @@ -1019,8 +1021,9 @@ golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220325170049-de3da57026de h1:pZB1TWnKi+o4bENlbzAgLrEbY4RMYmUIRobMcSmfeYc=
golang.org/x/net v0.0.0-20220325170049-de3da57026de/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down
1 change: 1 addition & 0 deletions parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,7 @@ func (n *ColumnPosition) Accept(v Visitor) (Node, bool) {
type AlterTableType int

// AlterTable types.
//nolint: deadcode
const (
AlterTableOption AlterTableType = iota + 1
AlterTableAddColumns
Expand Down